springboot集成mybatis實(shí)例代碼
springboot如何配置web項(xiàng)目請(qǐng)參考前一章,在此基礎(chǔ)上集成mybatis。
在pom文件中添加mybatis的依賴(lài):
<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依賴(lài),使用阿里巴巴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ù)條件查詢(xún),全部 --> <select id="searchAll" resultMap="BaseResultMap"> select * from tab_user </select> </mapper>
創(chuàng)建一個(gè)控制器,注入U(xiǎn)serDao,測(cè)試一下可不可以查詢(xún)數(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)成功后訪(fǎ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)文章
關(guān)于Jsoup將相對(duì)路徑轉(zhuǎn)為絕對(duì)路徑的方法
這篇文章主要介紹了關(guān)于Jsoup將相對(duì)路徑轉(zhuǎn)為絕對(duì)路徑的方法,jsoup 是一款Java 的HTML解析器,可直接解析某個(gè)URL地址、HTML文本內(nèi)容,需要的朋友可以參考下2023-04-04Java之SpringBoot定時(shí)任務(wù)案例講解
這篇文章主要介紹了Java之SpringBoot定時(shí)任務(wù)案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot+MinIO實(shí)現(xiàn)文件切片極速詳解
在現(xiàn)代Web應(yīng)用中,文件上傳是一個(gè)常見(jiàn)的需求,尤其是對(duì)于大文件的上傳,如視頻、音頻或大型文檔,所以本文就來(lái)為大家介紹一下如何使用Spring Boot和MinIO實(shí)現(xiàn)文件切片極速上傳技術(shù)吧2023-12-12SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的詳細(xì)流程
最近做了一個(gè)需求,消防的設(shè)備巡檢,如果巡檢發(fā)現(xiàn)異常,通過(guò)手機(jī)端提交,后臺(tái)的實(shí)時(shí)監(jiān)控頁(yè)面實(shí)時(shí)獲取到該設(shè)備的信息及位置,然后安排員工去處理。這篇文章主要介紹了SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的全過(guò)程,感興趣的朋友一起看看吧2021-10-10@PathParam和@QueryParam區(qū)別簡(jiǎn)析
這篇文章主要介紹了@PathParam和@QueryParam區(qū)別,分享了相關(guān)實(shí)例代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01