當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot与MyBatis技术集成
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot与MyBatis技术集成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 、創建project步驟
目錄結構
pom依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.william</groupId><artifactId>day01_springboot_mybatis</artifactId><version>0.0.1-SNAPSHOT</version><name>day01_springboot_mybatis</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!--spring MVC web開發支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--mybatis依賴起步坐標--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--數據庫驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
二、創建數據庫
-- ---------------------------- -- Table structure for `user` -- ----------------------------DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; -- ------------------------------ Records of user ------------------------------ INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '張三');INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');三、domain層 創建實體User
package com.william.domain;/*** @author :lijunxuan* @date :Created in 2019/6/27 20:41* @description :* @version: 1.0*/ public class User {private Integer id;private String username;//用戶名private String password;//密碼private String name;//姓名@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", name='" + name + '\'' +'}';}public Integer getId() {return id;}public void setId(Integer 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;}public String getName() {return name;}public void setName(String name) {this.name = name;} }四、 添加數據庫連接信息配置:在application.properties中添加數據庫連接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql://127.0.0.1/springboot?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC # mapper.xml配置文件路徑 mybatis.mapper-locations=classpath:mapper/*Mapper.xml #掃描實體 mybatis.type-aliases-package=com.william.domain五、controller層
package com.william.Controller;import com.william.Service.UserService; import com.william.domain.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource; import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/6/27 20:43* @description :* @version: 1.0*/ @RestController public class UserController {@ResourceUserService userService;@RequestMapping("/findAll")public List<User> findAll(){return userService.findAll();}}六、service層
UserService接口
package com.william.Service;import com.william.domain.User;import java.util.List;public interface UserService {List<User> findAll();}UserServiceImpl實現類
package com.william.Service.Impl;import com.william.Dao.UserMapper; import com.william.Service.UserService; import com.william.domain.User; import org.springframework.stereotype.Service;import javax.annotation.Resource; import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/6/27 20:48* @description :* @version: 1.0*/ @Service public class UserServiceImpl implements UserService {@ResourceUserMapper UserMapper;@Overridepublic List<User> findAll() {return UserMapper.findAll();} }七、DaoMapper接口層
package com.william.Dao;import com.william.domain.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository;import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/6/27 20:42* @description :* @version: 1.0*/ @Repository @Mapper // 編寫Mapper:使用@Mapper標記該類是一個Mapper接口,可以被SpringBoot自動掃描 public interface UserMapper {List<User> findAll(); }八、DaoMapper映射文件層
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.william.Dao.UserMapper"><select id="findAll" resultType="User">select * from user</select> </mapper>九、測試結果
總結
以上是生活随笔為你收集整理的SpringBoot与MyBatis技术集成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西昊电脑椅m18和m35(西昊m84椅子
- 下一篇: SpringBoot JPA