springboot集成mybatis實(shí)例代碼
springboot如何配置web項(xiàng)目請(qǐng)參考前一章,在此基礎(chǔ)上集成mybatis。
在pom文件中添加mybatis的依賴:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency>
添加mysql驅(qū)動(dòng):
<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>
配置數(shù)據(jù)源,在application.yml中:
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: 111111
# 使用druid數(shù)據(jù)源
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
設(shè)置mybatis的mapper和model掃描路徑:
mybatis: mapperLocations: classpath:mapper/*.xml typeAliasesPackage: com.yingxinhuitong.demo.model #更多配置請(qǐng)參見(jiàn):http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
接下來(lái)我們新建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" > <!-- 字段與實(shí)體的映射 --> <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> <!-- 根據(jù)條件查詢,全部 --> <select id="searchAll" resultMap="BaseResultMap"> select * from tab_user </select> </mapper>
創(chuàng)建一個(gè)控制器,注入U(xiǎn)serDao,測(cè)試一下可不可以查詢數(shù)據(jù)了:
@RestController
public class TestController {
@Resource
UserDao userDao;
@RequestMapping("/getusers")
public String test() {
List<UserEntity> users = userDao.searchAll();
String usersJson = JSON.toJSONString(users);
return usersJson;
}
}
運(yùn)行Application.class,啟動(dòng)成功后訪問(wèn):http://localhost:9000/demo/getusers,輸出內(nèi)容如下:
[{"id":1,"password":"000000","username":"test"},{"id":2,"password":"111111","username":"test1"},{"id":3,"password":"222222","username":"test2"}]
至此,springboot已完成對(duì)mybatis的集成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于Java實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
mybatis if test條件判斷語(yǔ)句中的判斷問(wèn)題分析
這篇文章主要介紹了mybatis if test條件判斷語(yǔ)句中的判斷問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java?FTP協(xié)議實(shí)現(xiàn)文件下載功能
FTP(File?Transfer?Protocol)就是文件傳輸協(xié)議。通過(guò)FTP客戶端從遠(yuǎn)程FTP服務(wù)器上拷貝文件到本地計(jì)算機(jī)稱為下載,將本地計(jì)算機(jī)上的文件復(fù)制到遠(yuǎn)程FTP服務(wù)器上稱為上傳,上傳和下載是FTP最常用的兩個(gè)功能2022-11-11

