springboot如何配置web項目請參考前一章,在此基礎上集成mybatis。java
在pom文件中添加mybatis的依賴:mysql
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency>
添加mysql驅動:web
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
添加druid和fastjson依賴,使用阿里巴巴druid鏈接池spring
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.28</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency>
配置數據源,在application.yml中:sql
spring: datasource: name: test url: jdbc:mysql://127.0.0.1:3306/test username: root password: 111111 # 使用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和model掃描路徑:apache
mybatis: mapperLocations: classpath:mapper/*.xml typeAliasesPackage: com.yingxinhuitong.demo.model
#更多配置請參見:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
接下來咱們新建userMapper.xml,UserEntity以及UserDao:json
1 package com.yingxinhuitong.demo.model; 2 3 /** 4 * Created by jack on 2017/4/20. 5 */ 6 public class UserEntity { 7 private Long id; 8 private String username; 9 private String password; 10 11 public Long getId() { 12 return id; 13 } 14 15 public void setId(Long id) { 16 this.id = id; 17 } 18 19 public String getUsername() { 20 return username; 21 } 22 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 }
package com.yingxinhuitong.demo.dao; import com.yingxinhuitong.demo.model.UserEntity; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * Created by jack on 2017/4/20. */ @Mapper public interface UserDao { List<UserEntity> searchAll(); }
<?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.yingxinhuitong.demo.dao.UserDao" > <!-- 字段與實體的映射 --> <resultMap id="BaseResultMap" type="com.yingxinhuitong.demo.model.UserEntity"> <id column="id" property="id" jdbcType="BIGINT" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> </resultMap> <!-- 根據條件查詢,所有 --> <select id="searchAll" resultMap="BaseResultMap"> select * from tab_user </select> </mapper>
建立一個控制器,注入UserDao,測試一下可不能夠查詢數據了:springboot
1 @RestController 2 public class TestController { 3 4 @Resource 5 UserDao userDao; 6 7 @RequestMapping("/getusers") 8 public String test() { 9 List<UserEntity> users = userDao.searchAll(); 10 String usersJson = JSON.toJSONString(users); 11 return usersJson; 12 } 13 }
運行Application.class,啓動成功後訪問:http://localhost:9000/demo/getusers,輸出內容以下:微信
[{"id":1,"password":"000000","username":"test"},{"id":2,"password":"111111","username":"test1"},{"id":3,"password":"222222","username":"test2"}]
至此,springboot已完成對mybatis的集成。mybatis
關注老薑談技術,微信號:helojava,或者掃描下面二維碼。
代碼改變世界。