先上https://start.spring.io/上創(chuàng)建項目,添加mysql,mybatis依賴,maven文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zdt</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml配置文件,設置數(shù)據(jù)源和mybatis
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
在mysql 中創(chuàng)建一張user 表,包含id,name,age字段
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`age` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
創(chuàng)建mapper,service,service.impl,pojo,controller幾個包
必須在啟動的application類上加上掃描mapper的注解,否則會找不到mapper
@SpringBootApplication
@MapperScan(basePackages = "com.zdt.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在pojo包下創(chuàng)建User,最好是沒有構造函數(shù),否則會報錯,原因暫切不明。
public class User {
private Long id;
private String name;
private Integer age;
// public User(String name,int age){
// this.name=name;
// this.age=age;
// }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
在mapper包下創(chuàng)建UserMapper來直接訪問數(shù)據(jù)庫,加上mapper注解
@Mapper
public interface UserMapper extends Mapper {
@Select("select * from user where id = #{id}")
public User getUser(Long id );
}
service包下創(chuàng)建UserService接口
public interface UserService {
public User getUserById(Long id);
}
impl下創(chuàng)建UserServiceImpl實現(xiàn)UserService接口,加上service 注解 并注入userMapper
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Long id) {
return userMapper.getUser(id);
}
}
最后就可以在控制器中調用userService中的getUserByid來獲取用戶數(shù)據(jù)了,請事先在數(shù)據(jù)庫添加一下相應的記錄
@RestController
public class Hello {
@Autowired
private UserService userService;
@GetMapping("/hello")
public User index(){
return userService.getUserById(1l);
}
}
瀏覽器訪問http://localhost:8080/hello 就可以查看到用戶記錄了。
如果要使用XML來配置mapper,則配置中要增加mapper的xml路徑:
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.zdt.demo.pojo
在資源文件夾中創(chuàng)建mapper文件夾,并創(chuàng)建UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zdt.demo.mapper.UserMapper">
<select id="findAll" resultType="User">
SELECT * FROM user
</select>
<select id="getUserById" resultType="com.zdt.demo.pojo.User" >
SELECT * FROM user WHERE id=#{id}
</select>
</mapper>
mapper中增加 新的方法
@Mapper
public interface UserMapper extends Mapper {
public User getUserById(Long id);
public List<User> findAll();
}
然后service 中就可以調用新增的方法了。