简介:
目前,国内大部分公司都使用 MyBatis作为持久层框架。本章整合MyBatis,在上一章的基础上进行扩展。废话少说,直接上代码。
pom 主要依赖如下:
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring-boot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
配置文件:
application.properties( 或 .yml 文件)
server.context-path=/
server.port=8080
# 数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
## Mybatis 配置
mybatis.typeAliasesPackage=com.coolron.*.domain
mybatis.mapperLocations=classpath:mapping/*/*.xml
入口类:
// mapper 接口类扫描包配置
@MapperScan("com.coolron.*.dao")
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
@MapperScan 注解:指定 mapper(dao) 接口对应的包位置。若未配置会出现如下异常:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.coolron.user.service.impl.UserServiceImpl required a bean of type 'com.coolron.user.dao.UserMapper' that could not be found.
此处应特别注意入口类 SpringbootApplication 所在包的位置。因为包扫描需要自定义,否则扫描到的只是入口类所在的包,或者入口类所在包的下一级。访问资源报错 404。
方案一:
入口类注解指定包扫面位置:
@SpringBootApplication(scanBasePackages = "com.coolron")
方案二 :
模块入口类放到所有需要扫描到的类的上级包中。
controller测试:
@RestController
@RequestMapping(value = "user")
public class UserController {
@Autowired
private UserService userService;
/**
* 通过用户 id 获取用户信息
* @param id 用户id
* @return
*/
@GetMapping(value = "getInfo/{id}")
public User getInfo(@PathVariable("id") Integer id) {
return userService.getInfo(id);
}
}
service(service接口省略):
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper mapper;
@Override
public User getInfo(Integer id) {
return mapper.selectByPrimaryKey(id);
}
}
mapper 以及mapper.xml :
此处自动生成后截取了一部分
public interface UserMapper {
User selectByPrimaryKey(Integer id);
}
<?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.coolron.user.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.coolron.user.domain.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="cityId" jdbcType="INTEGER" property="cityId" />
</resultMap>
<sql id="Base_Column_List">
id, age, name, password, description, cityId
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
使用自动生成插件生成(查阅mybatis自动生成代码相关资料)。
项目启动、测试:
同第一章运行入口类 main 函数即可启动。
Get请求可以使用浏览器输入:http://localhost:8080/user/getInfo/3
说明:
此博客主要是以学习、分享为主。互联网相关的文章很多,如有雷同,还望包涵,如有错误,希望留言或联系我指出。
联系我:
QQ:1421925880
相关springboot、springcloud、docker等文章关注微信公众号: