开发环境
- IntelliJ IDEA 2018.2.3 x64
- JDK 1.8_144
- Spring Boot、MyBatis、Maven
导入maven依赖
Spring Boot版本
<!--Spring Boot的父依赖,下面的依赖都继承该依赖。(可省略version版本)-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
MyBatis于mysql驱动版本
<!-- 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>
<version>5.1.6</version>
</dependency>
项目结构如下:
application.properties配置文件设置数据源以及mybatis的常用配置:
# tomcat端口
server.port=80
# tomcat路径名
server.servlet.context-path=/springbootdemo# 配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource## 配置mybatis别名和映射文件
mybatis.type-aliases-package=com.wu.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
application.yml配置文件设置数据源以及mybatis的常用配置:
spring:# 配置数据源 datasource: url: jdbc:mysql://localhost:3306/test driver-class-name: com.mysql.jdbc.Driver username: root password: root type: com.alibaba.druid.pool.DruidDataSource # 配置mybatis别名和映射文件 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.wu.pojo
编写实体类 City:
public class City {
private Integer id;
private Integer pid;
private String cityName;
private Integer type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
编写dao层接口 CityDao:
@Service
public interface CityDao {
/**
* 查询所有的城市
* @return
*/
List<City> selectAll();
City selectById(Integer id);
}
编写mybatis映射文件 CityMapper.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.wu.dao.CityDao">
<!-- city表所有字段映射 -->
<resultMap id="cityColumnAllResultMap" type="com.wu.pojo.City">
<id column="id" property="id"></id>
<result column="pid" property="pid"></result>
<result column="cityname" property="cityname"></result>
<result column="type" property="type"></result>
</resultMap>
<!-- city所有字段 -->
<sql id="cityColumnAll">
id,
pid,
cityname,
type
</sql>
<!-- 查询所有的城市 -->
<select id="selectAll" resultMap="cityColumnAllResultMap">
select <include refid="cityColumnAll"></include> from city
</select>
</mapper>
编写service层 接口 CityService :
public interface CityService {
/**
* 查询所有的城市
* @return
*/
List<City> selectAll();
City selectById(Integer id);
}
编写service层实现接口类 CityServiceImpl:
@Service(value = "cityService")
public class CityServiceImpl implements CityService {
@Resource
private CityDao cityDao;
/**
* 查询所有的城市
*
* @return
*/
@Override
public List<City> selectAll() {
return cityDao.selectAll();
}
@Override
public City selectById(Integer id) {
return cityDao.selectById(id);
}
}
控制器类 TestController:
@RestController
public class TestController {
@Resource
private RedisUtils redisUtils;
@Autowired
private CityService cityService;
@Resource
private MysqlProperties mysqlProperties;
@RequestMapping(value = "mybatis")
public String textMybatis(){
List<City> cities = cityService.selectAll();
System.out.println(cities);
return ""+cities;
}
}
主程序类 SpringbootdemoApplication :
@SpringBootApplication
@ComponentScan(basePackages = {"com.wu"})
@MapperScan(value = "com.wu.dao")
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
测试MyBatis :
测试结果: