SpringBoot專案中整合Mybatis框架
阿新 • • 發佈:2020-10-07
目錄
一、新增pom座標
<!--mybatis起步依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!--MySQL驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
二、配置資訊
1.application.properties配置資料庫連線資訊
#資料庫連線資訊
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
2.application.properties配置mybatis的對映檔案路徑與實體類別名
- 此處配置可以使用註解替代
#spring整合Mybatis環境
#pojo別名掃描包
mybatis.type-aliases-package=com.wuyiz.springboot_quick.entity
#載入Mybatis對映檔案
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
三、建立類測試是否搭建成功
1.整體檔案結構展示
2.Controller
@RestController public class UserController { @Autowired private UserService userService; @RequestMapping("queryUser") public List<User> queryUser(){ List<User> users = userService.queryUser(); return users; } }
3.Service介面和實現類
//介面
public interface UserService {
List<User> queryUser();
}
//實現類
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> queryUser() {
return userMapper.queryUserList();
}
}
4.Mapper介面
@Mapper
@Repository //把mapper介面註冊為spring的bean,避免@autowired時爆紅(無影響)
public interface UserMapper {
public List<User> queryUserList();
}
5.Mapper對映檔案
<?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.wuyiz.springboot_quick.mapper.UserMapper">
<resultMap id="BaseResultMap" type="User">
<id column="id" jdbcType="INTEGER" property="id"/>
<id column="username" jdbcType="VARCHAR" property="username"/>
<id column="birthday" jdbcType="TIMESTAMP" property="birthday"/>
<id column="sex" jdbcType="VARCHAR" property="sex"/>
<id column="address" jdbcType="VARCHAR" property="address"/>
</resultMap>
<sql id="Base_Column_List">
id, username, birthday, sex, address
</sql>
<select id="queryUserList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
</select>
</mapper>
6.實體類以及資料庫表
//實體類
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
//getter和setter....
}
-- user資料表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '使用者名稱稱',
`birthday` datetime NULL DEFAULT NULL COMMENT '生日',
`sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性別',
`address` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 60 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;