一、引入依赖
<!-- 整合mybatis 开始-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- 整合mybatis 结束-->
二、配置链接信息
类似于spring MVC,我们需要在application.yml中配置数据库的连接信息
spring:
#数据库链接信息配置
datasource:
url: jdbc:mysql://localhost:3306/pullulate
username: root
password:
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mybatis/system/*Mapper.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: top.pullulates.project.system # 注意:对应实体类的路径
config-location: classpath:mybatis/common/mybatis-config.xml
mybatis-config.xml 是mybatis的配置文件,其内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
目前尚未做任何配置,后期会添加。
注意:springboot里,mybatis是默认开启事务的,这里不用再去配置啦,也不进行详细说明。
三、生成对应的Model、mapper内容
这里不做介绍,具体请参考我的博客:Spring boot使用Generator自动生成代码
四、编写代码进行测试
1. controller、 service和impl
/**
* HelloWorld
*
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/")
public String hello() {
List<User> users = userService.selectAll();
return users.toString();
}
}
/**
* 用户服务接口
*
*/
public interface UserService {
/**
* 查询所有的用户
*
* @return
*/
List<User> selectAll();
}
/**
* 用户服务接口的实现方法
*
*/
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 查询所有的用户信息
*
* @return
*/
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
}
2. mapper接口及xml
public interface UserMapper {
/**
* 查询所有的用户信息
*
* @return
*/
List<User> selectAll();
}
<sql id="Base_Column_List" >
id, user_id, role_id, acount_id, user_name, phone, password, email, contact_name,
id_card, bank, address, industry, company, head_img, error_login, last_login, token,
third_acount, qq, sex, marriage, status, add_user, add_time, update_user, update_time, back_reason,
attr
</sql>
<!-- 查询所有的用户信息 -->
<select id="selectAll" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from sys_user
</select>
3. 注册mapper
spring boot里,只需要在入口程序里定义mapper的范围就可以成功注册mapper啦
@SpringBootApplication
@EnableConfigurationProperties({Pullulates.class, Student.class})
@MapperScan("top.pullulates.project.*.*.mapper")
public class PullulatesApplication {
public static void main(String[] args) {
//新的启动方式
// SpringApplication springApplication = new SpringApplication(PullulatesApplication.class);
// //隐藏banner
// springApplication.setBannerMode(Banner.Mode.OFF.);
// springApplication.run(args);
//原来的启动方式,显示banner
SpringApplication.run(PullulatesApplication.class, args);
}
}
这里晒一下目录结构,方便小伙伴们对比一下:
五、运行服务
运行项目,访问,浏览器出现如下内容
[User{id=100000000, userId='100000000', roleId=100000000, acountId=100000000, userName='pullulates', phone='182979', password='96e79218965eb72c92a549dd5a330112', email='27889923@qq.com', contactName='t1heluosh1', idCard='340111111111111111', bank='null', address='null', industry='null', company='pullulates', headImg='null', errorLogin=0, lastLogin=Thu Oct 11 15:07:45 CST 2018, token='null', thirdAcount=null, qq='null', sex=100, marriage=null, status=100, addUser='100000000', addTime=Thu Oct 11 15:09:06 CST 2018, updateUser='100000000', updateTime=Thu Oct 11 15:09:14 CST 2018, attr='备用字段', backReason='null'}]