1. 新建工程,选择web,Mysql,JDBC,MyBatis模块
查看依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<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>1.3.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>
2. 删除application.properties,新建application.yml,内容如下。
spring:
datasource:
username: root
password: 12345678
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8090
mybatis:
configuration:
#开启驼峰命名转换
map-underscore-to-camel-case: true
3. 启动mysql,创建用户表
(1)在cmd窗口 输入 net start mysql
mysql -u root –p
(2)新建数据库 create database mybatis;
(3)新建用户表
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(60) NOT NULL,
`employeeNum` varchar(255) NOT NULL,
`departmentId` int(11) NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1',
`createTime` datetime NOT NULL,
`updateTime` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY ` employeeNum` (`employeeNum`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
insert into t_user(username,password,employeeNum,departmentId) values('aa','1','001',3);
insert into t_user(username,password,employeeNum,departmentId) values('bb','1','002',4);
insert into t_user(username,password,employeeNum,departmentId) values('cc','1','003',5);
insert into t_user(username,password,employeeNum,departmentId) values('dd','1','004',4);
insert into t_user(username,password,employeeNum,departmentId) values('ee','1','005',6);
4. 创建bean.User 类
Mac 电脑 control + return 调用get,set方法
public class User {
private Integer id;
private String username;
private String password;
private String employeeNum;
private Integer departmentId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmployeeNum() {
return employeeNum;
}
public void setEmployeeNum(String employeeNum) {
this.employeeNum = employeeNum;
}
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
}
5. 创建mapper.UserMapper
@Mapper
public interface UserMapper {
@Select("select * from t_user where id = #{id}")
public User getUserById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id") //加入自增主键
@Insert("insert into t_user(username,password,employeeNum,departmentId) values(#{username},#{password},#{employeeNum},#{departmentId})")
public int insertUser(User user);
@Delete("delete from t_user where id=#{id}")
public int deleteById(Integer id);
@Update("update t_user set username=#{username},password=#{password} where id=#{id}")
public int updateUser(User user);
@Select("select * from t_user")
public List<User> getALLUsers();
}
5.添加controller.UserController
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Integer id){
return userMapper.getUserById(id);
}
@GetMapping("userlist")
public List<User> getUsers(){
return userMapper.getALLUsers();
}
}
6. 运行 http://localhost:8090/user/4
运行 http://localhost:8090/userlist
以上完成了mybatis的操作,下面是几个其他操作:
1.id显示为null,修改mapper,加入Option
@Options(useGeneratedKeys = true,keyProperty = "id") //加入自增主键
@Insert("insert into t_user(username,password,employeeNum,departmentId) values(#{username},#{password},#{employeeNum},#{departmentId}")
2. 驼峰命名转换:自定义MyBatis的配置规则,给容器里添加一个ConfigurationCustomizer组件
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
或者采用以下进行驼峰命名转换
mybatis:
configuration:
#开启驼峰命名转换
map-underscore-to-camel-case: true
3. 使用MapperScan批量扫描所有的Mapper接口
给每个mapper里添加@Mapper注解进行自动装配太麻烦了
可以在主类里添加@MapperScan(value=”com.lulu.mappper”)
@MapperScan(value=”com.lulu.mappper”)
@SpringBootApplication
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}