SpringBoot 整合 Mybatis 数据库持久层
SpringBoot 整合 Mybatis 数据库持久层
一:springboot 整合 mybatis
1:项目过程结构:
2:pom.xml 整合配置
<?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 https://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.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alanCode</groupId> <artifactId>SpringBootMyBatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootMyBatis</name> <description>SpringBootMyBatis</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- springBoot 的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- web 启动器 --> <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> <!-- mysql 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- druid 数据库连接池 这里用的是最新的 数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
说明:
以下这段内容是用来解决idea 开发工具中的 maven 对代码层 里的 mapper 包里的 mybatis 配置文件进行编译打包配置内容。不然在启动编译的过程中,会丢失 该配置文件而在进行数据库操作时候报错。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
3:启动空的项目代码:
二:进行数据库插入数据操作。
1:创建数据库脚本:(mysql数据库)
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建数据库 名称:
2:创建数据库链接及其其他配置 application.properties
### 此版本为mysql 5.X 的 sql连接驱动器使用方法### # spring.datasource.driverClassName=com.mysql.jdbc.Driver ### 此版本为mysql 6.X 以上 的 sql连接驱动器使用方法### spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.type-aliases-package=com.alanCode.pojo
3:创建:Users 对象
package com.alanCode.pojo; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:27 */ public class Users { private Integer id; private String name; private Integer age; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Users{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
4:创建userMapper 对象
UsersMapper.java
package com.alanCode.mapper; import com.alanCode.pojo.Users; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:38 */ public interface UsersMapper { void insertUser(Users users); void insertUser1(Users users); }
UsersMapper.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.alanCode.mapper.UsersMapper"> <insert id="insertUser" parameterType="users"> insert into users(name,age) values(#{name},#{age}) </insert> <insert id="insertUser1" parameterType="users"> insert into users(name,age) values(#{name},#{age}) </insert> </mapper>
5:创建:UsersService
package com.alanCode.service; import com.alanCode.pojo.Users; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:29 */ public interface UsersService { void addUser(Users users); }
6:创建:UsersServiceImpl
package com.alanCode.service.Imp; import com.alanCode.pojo.Users; import com.alanCode.mapper.UsersMapper; import com.alanCode.service.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:40 */ @Service @Transactional public class UsersServiceImpl implements UsersService{ @Autowired private UsersMapper usersMapper; /** * * @param users */ @Override public void addUser(Users users) { this.usersMapper.insertUser1(users); } }
7:创建 controller
package com.alanCode.controller; import com.alanCode.pojo.Users; import com.alanCode.service.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:55 */ @Controller @RequestMapping("/users") public class UsersController { @Autowired private UsersService usersService; @RequestMapping("/{page}") public String showPage(@PathVariable String page){ return page; } @RequestMapping("/addUser") public String addUser(Users users){ this.usersService.addUser(users); return "ok"; } }
8:创建 页面
input.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加用户</title> </head> <body> <form th:action="@{/users/addUser}" method="post"> 用户姓名:<input type="text" name="name"/><br/> 用户年龄:<input type="text" name="age"/><br/> <input type="submit" value="确定"/><br/> </form> </body> </html>
ok.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>操作提示页面</title> </head> <body> 操作成功!!! </body> </html>
9:启动应用:app
package com.alanCode; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.alanCode.mapper") //@MapperScan 用于扫描mybatis的mapper接口文件而非mapper.xml 文件 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
10:代码结构:
11:界面访问:
12:查看数据库数据情况
三:完整代码及其执行效果
1:查看效果图
列表展示数据
新增 数据: 点击新增按钮
点击确定 ,返回数据列表
修改数据: 点击“更新用户 ”按钮
点击 确定按钮 进行数据列表界面
删除数据:点击 删除用户 按钮
2:完整数据展示
pom.xml
<?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 https://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.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alanCode</groupId> <artifactId>SpringBootMyBatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootMyBatis</name> <description>SpringBootMyBatis</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- springBoot 的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- web 启动器 --> <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> </dependency> <!-- mysql 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- druid 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
UsersController
package com.alanCode.controller; import com.alanCode.pojo.Users; import com.alanCode.service.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:55 */ @Controller @RequestMapping("/users") public class UsersController { @Autowired private UsersService usersService; /** * 页面跳转 * *@Param: [page] *@return *@throws *@author Alan_Liu *@date 2021/3/28 22:50 * **/ @RequestMapping("/{page}") public String showPage(@PathVariable String page) { return page; } /** * * 添加 用户 *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/28 22:51 * **/ @RequestMapping("/addUser") public String addUser(Users users) { this.usersService.addUser(users); return "forward:/users/findUserAll";//重定向转发 } /** *查询全部用户 * *@Param: [model] *@return *@throws *@author Alan_Liu *@date 2021/3/28 22:54 * **/ @RequestMapping("/findUserAll") public String findUserAll(Model model,Users users){ List<Users> listUsers=this.usersService.findUserAll(users); model.addAttribute("list",listUsers); return "showUsers"; } /*** *更新数据回显 重新1个数据用户信息 * *@Param: [id, model] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:42 * **/ @RequestMapping("/findUserById") public String findUSerById(Integer id,Model model){ Users user=this.usersService.findUserById(id); model.addAttribute("user",user); return "updateUser"; } /*** * * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/29 0:13 * **/ @RequestMapping("/editUser") public String editUser(Users users){ this.usersService.updateUser(users); return "forward:/users/findUserAll";//重定向转发 } /** * 删除用户信息 * *@Param: [id, model] *@throws *@author Alan_Liu *@date 2021/3/29 0:22 * **/ @RequestMapping("/deleteUser") public String deleteUser(Integer id, Model model){ this.usersService.deleteUser(id); return "forward:/users/findUserAll";//重定向转发 } }
UsersMapper
package com.alanCode.mapper; import com.alanCode.pojo.Users; import java.util.List; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:38 */ public interface UsersMapper { /** * * 添加 用户信息 * @author Admin * @date 2021/03/26 17:22 * @param users * @return void */ void insertUser(Users users); /*** *保存更新的数据信息 * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/29 0:10 * **/ void updateUser(Users users); /*** * 删除用户数据信息 * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/29 0:10 * **/ void deleteUser(Integer id); /** * 重新 用户所有数据信息 * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:33 * **/ List<Users> selectUserAll(Users users); /** *根据id查询 用户信息 * *@Param: [id] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:33 * **/ Users selectUserById(Integer id); }
UsersMapper.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.alanCode.mapper.UsersMapper"> <insert id="insertUser" parameterType="users"> insert into users(name, age) values (#{name}, #{age}) </insert> <select id="selectUserAll" resultType="com.alanCode.pojo.Users"> select id,name,age from users </select> <select id="selectUserById" resultType="com.alanCode.pojo.Users"> select id,name,age from users where id =#{value} </select> <insert id="updateUser" parameterType="users"> update users set name =#{name} ,age=#{age} where id=#{id} </insert> <delete id="deleteUser" > delete from users where id=#{value} </delete> </mapper>
Users
package com.alanCode.pojo; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:27 */ public class Users { private Integer id; private String name; private Integer age; public Users() { } public Users(Integer id) { this.id = id; } public Users(Integer id, String name) { this.id = id; this.name = name; } public Users(String name, Integer age) { this.name = name; this.age = age; } public Users(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Users{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
UsersService
package com.alanCode.service; import com.alanCode.pojo.Users; import java.net.UnknownServiceException; import java.util.List; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:29 */ public interface UsersService { /** * * 添加 用户信息 * @author Admin * @date 2021/03/26 17:22 * @param users * @return void */ void addUser(Users users); /** * 重新 用户所有数据信息 * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:33 * **/ List<Users> findUserAll(Users users) ; /** *根据id查询 用户信息 * *@Param: [id] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:33 * **/ Users findUserById(Integer id); /** * * 保存更新数据记录 *@Param: [] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:47 * **/ void updateUser(Users users); /** * 删除用户信息 * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/29 0:19 * **/ void deleteUser( Integer id); }
UsersServiceImpl
package com.alanCode.service.Imp; import com.alanCode.pojo.Users; import com.alanCode.mapper.UsersMapper; import com.alanCode.service.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * @author Alan -liu * @Email * @Web: * @Create 2021-03-26 9:40 */ @Service @Transactional public class UsersServiceImpl implements UsersService { @Autowired private UsersMapper usersMapper; /** * * 添加 用户信息 * @author Admin * @date 2021/03/26 17:22 * @param users * @return void */ @Override public void addUser(Users users) { this.usersMapper.insertUser(users); } /** * 重新 用户所有数据信息 * *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:33 * **/ @Override public List<Users> findUserAll(Users users) { return this.usersMapper.selectUserAll(users); } /** *根据id查询 用户信息 * *@Param: [id] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:33 * **/ @Override public Users findUserById(Integer id) { return this.usersMapper.selectUserById(id); } /** * * 保存数据更新 *@Param: [] *@return *@throws *@author Alan_Liu *@date 2021/3/28 23:47 * **/ @Override public void updateUser(Users users) { this.usersMapper.updateUser(users); } /** * * 删除用户 *@Param: [users] *@return *@throws *@author Alan_Liu *@date 2021/3/29 0:20 * **/ @Override public void deleteUser(Integer id) { this.usersMapper.deleteUser(id); } }
App
package com.alanCode; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.lang.reflect.Field; /** * *@author Alan_Liu *@date 2021/3/28 23:35 * **/ @SpringBootApplication @MapperScan("com.alanCode.mapper") // @MapperScan 用于扫描mybatis的mapper接口文件而非mapper.xml 文件 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
application.properties
### 此版本为mysql 5.X 的 sql连接驱动器使用方法### # spring.datasource.driverClassName=com.mysql.jdbc.Driver ### 此版本为mysql 6.X 以上 的 sql连接驱动器使用方法### spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.type-aliases-package=com.alanCode.pojo
input.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加用户</title> </head> <body> <form th:action="@{/users/addUser}" method="post"> 用户姓名:<input type="text" name="name"/><br/> 用户年龄:<input type="text" name="age"/><br/> <input type="submit" value="确定"/><br/> </form> </body> </html>
ok.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>操作提示页面</title> </head> <body> 操作成功!!! </body> </html>
showUsers.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>showUsers 展示用户数据</title> </head> <body> <a th:href="@{/users/input}">新增</a> <table border="1" style="width:300px;"> <tr> <th>用户ID</th> <th>用户姓名</th> <th>用户年龄</th> <th>操作</th> </tr> <tr th:each="user : ${list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td> <a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a> <a th:href="@{/users/deleteUser(id=${user.id})}">删除用户</a> </td> </tr> </table> </body> </html>
updateUser.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>更新用户信息</title> </head> <body> <form th:action="@{/users/editUser}" method="post"> <input type="hidden" name="id" th:field="${user.id}"/><br/> 用户姓名:<input type="text" name="name" th:field="${user.name}"/><br/> 用户年龄:<input type="text" name="age" th:field="${user.age}"/><br/> <input type="submit" value="确定"/><br/> </form> </body> </html>
3:代码结构图
### 此版本为mysql 5.X 的 sql连接驱动器使用方法###
# spring.datasource.driverClassName=com.mysql.jdbc.Driver
### 此版本为mysql 6.X 以上 的 sql连接驱动器使用方法###
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.alanCode.pojo
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/