您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用springboot如何實現對mybatis集成,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
在pom文件中添加mybatis的依賴:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency>
添加mysql驅動:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
添加druid和fastjson依賴,使用阿里巴巴druid連接池
<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中:
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掃描路徑:
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:
UserEntity.class
package com.yingxinhuitong.demo.model; /** * Created by jack on 2017/4/20. */ public class UserEntity { private Long id; private String username; private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
UserDao
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(); }
UserMapper.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.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,測試一下可不可以查詢數據了:
@RestController public class TestController { @Resource UserDao userDao; @RequestMapping("/getusers") public String test() { List<UserEntity> users = userDao.searchAll(); String usersJson = JSON.toJSONString(users); return usersJson; } }
運行Application.class,啟動成功后訪問:http://localhost:9000/demo/getusers,輸出內容如下:
至此,springboot已完成對mybatis的集成。
上述內容就是使用springboot如何實現對mybatis集成,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。