當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
016_SpringBoot整合MyBatis
生活随笔
收集整理的這篇文章主要介紹了
016_SpringBoot整合MyBatis
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 使用maven構建SpringBoot的名叫spring-boot-mybatis項目
2. pom.xml?
<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><groupId>com.bjbs</groupId><artifactId>spring-boot-mybatis</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標簽不規(guī)范報錯 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- Mybatis啟動器 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.2</version></dependency></dependencies> </project>3. 在src/main/resources下創(chuàng)建全局配置文件application.properties
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.25.138:3306/StudyMybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=lyw123456spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource4. 數(shù)據(jù)庫表設計
5. 新建User.java?
package com.bjbs.pojo;import java.io.Serializable; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private String sex;@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")private Date birthday;private String address;public User() {}public User(String name, String sex, Date birthday, String address) {this.name = name;this.sex = sex;this.birthday = birthday;this.address = address;}public User(Integer id, String name, String sex, Date birthday, String address) {this.id = id;this.name = name;this.sex = sex;this.birthday = birthday;this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address+ "]";}}6. 新建UserMapper.java
package com.bjbs.mapper;import java.util.List; import com.bjbs.pojo.User;public interface UserMapper {public List<User> findAllUser();void saveUser(User user);User findUserById(Integer id);void updateUser(User user);void deleteUserById(Integer id); }7. 新建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.bjbs.mapper.UserMapper"><select id="findAllUser" resultType="com.bjbs.pojo.User">select * from user</select><insert id="saveUser" parameterType="com.bjbs.pojo.User">insert into user values(null, #{name}, #{sex}, #{birthday}, #{address})</insert><select id="findUserById" resultType="com.bjbs.pojo.User">select * from user where id = #{value}</select><update id="updateUser" parameterType="com.bjbs.pojo.User">update user set name=#{name}, sex = #{sex}, birthday = #{birthday}, address = #{address} where id=#{id}</update><delete id="deleteUserById">delete from user where id = #{value}</delete></mapper>8. 新建UserService.java
package com.bjbs.service;import java.util.List; import com.bjbs.pojo.User;public interface UserService {List<User> findAllUser();void saveUser(User user);User findUserById(Integer id);void updateUser(User user);void deleteUserById(Integer id); }9. 新建UserServiceImpl.java
package com.bjbs.service.impl;import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.bjbs.mapper.UserMapper; import com.bjbs.pojo.User; import com.bjbs.service.UserService;@Service @Transactional public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> findAllUser() {return userMapper.findAllUser();}@Overridepublic void saveUser(User user) {userMapper.saveUser(user);}@Overridepublic User findUserById(Integer id) {return userMapper.findUserById(id);}@Overridepublic void updateUser(User user) {userMapper.updateUser(user);}@Overridepublic void deleteUserById(Integer id) {userMapper.deleteUserById(id);}}10. 新建App.java
package com.bjbs;import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動類*/ @SpringBootApplication @MapperScan("com.bjbs.mapper") //@MapperScan 用戶掃描MyBatis的Mapper接口 public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }11. 在src/main/resources/templates下, 新建allUser.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>展示用戶數(shù)據(jù)</title><style type="text/css">th {width: 100px;}</style></head><body><table border="1"><tr><th>用戶ID</th><th>用戶姓名</th><th>用戶性別</th><th style="width: 300px;">用戶生日</th><th>用戶地址</th><th style="width: 300px;">操作</th></tr><tr th:each="user : ${list}"><td th:text="${user.id}"></td><td th:text="${user.name}"></td><td th:text="${user.sex}"></td><td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd HH:mm:ss')}"></td><td th:text="${user.address}"></td><td><a th:href="@{/findUserById(id=${user.id})}">更新用戶</a><a th:href="@{/delUser(id=${user.id})}">刪除用戶</a><a th:href="@{/addUser}">添加用戶</a></td></tr></table></body> </html>12. 在src/main/resources/templates下, 新建addUser.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>添加用戶</title></head><body><form th:action="@{/saveUser}" method="post">用戶姓名: <input type="text" name="name"/><br/>用戶性別: <input type="text" name="sex"/><br/>用戶生日: <input type="text" name="birthday"/><br/>用戶地址: <input type="text" name="address"/><br/><input type="submit" value="確定"/><br/></form></body> </html>13. 在src/main/resources/templates下, 新建editUser.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>更新用戶信息</title></head><body><form th:action="@{/updateUser}" method="post"><input type="hidden" name="id" th:field="${user.id}"/>用戶姓名: <input type="text" name="name" th:field="${user.name}"/><br/>用戶性別: <input type="text" name="sex" th:field="${user.sex}"/><br/>用戶生日: <input type="text" name="birthday" th:field="${user.birthday}"/><br/>用戶地址: <input type="text" name="address" th:field="${user.address}"/><br/><input type="submit" value="確定"/><br/></form></body> </html>14. 運行項目并使用瀏覽器訪問
15. 點擊添加用戶鏈接, 跳轉(zhuǎn)到添加用戶頁面?
16. 添加一個ID為41的用戶?
17. 點擊ID為40的更新用戶鏈接, 跳轉(zhuǎn)到更新用戶頁面?
18. 修改用戶信息
19. 點擊確定按鈕, 保存用戶信息
20. 點擊ID為39的刪除用戶鏈接, 刪除用戶成功?
總結
以上是生活随笔為你收集整理的016_SpringBoot整合MyBatis的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 015_SpringBoot视图层技术t
- 下一篇: 017_SpringBoot异常处理方式