Spring Boot结合Mybatis
2024-09-01 05:38:29
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.9.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>11-spring-boot-springmvc-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- thymeleaf热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</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.1.10</version>
</dependency>
<!--JDBC-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>-->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
application.properties
#项目端口配置
server.port=8080
server.address=0.0.0.0
#Mysql数据源配置
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.bjsxt.pojo
mapper.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.bjsxt.mapper.UsersMapper">
<!--添加-->
<insert id="insertUser" parameterType="users">
insert into users(username,userage) values (#{username},#{userage})
</insert>
<!--查询所有-->
<select id="selectUsers" resultType="users">
select * from users
</select>
<!--根据ID查询-->
<select id="selectUserByUserid" resultType="users">
select * from users where userid=#{value}
</select>
<!--更新-->
<update id="updateUser" parameterType="users">
update users set username=#{username},userage=#{userage} where userid=#{userid}
</update>
<!--删除-->
<delete id="delectUser" >
delete from users where userid=#{value}
</delete>
</mapper>
实体类
package com.bjsxt.pojo;
import java.io.Serializable;
public class Users implements Serializable {
private int userid;
private String username;
private int userage;
public Users(int userid, String username, int userage) {
this.userid = userid;
this.username = username;
this.userage = userage;
}
public Users(){}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
@Override
public String toString() {
return "Users{" +
"userid=" + userid +
", username='" + username + '\'' +
", userage=" + userage +
'}';
}
}
service接口
package com.bjsxt.service;
import com.bjsxt.pojo.Users;
import java.util.List;
public interface UsersService {
//添加用户
void addUser(Users users);
//查询用户
List<Users> findAll();
//根据ID查询用户
Users findUserById(int userid);
//更新
void updateUser(Users users);
//删除
void removeUser(int userid);
}
service实现类
package com.bjsxt.service.impl;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
this.usersMapper.insertUser(users);
}
@Override
public List<Users> findAll() {
return this.usersMapper.selectUsers();
}
@Override
public Users findUserById(int userid) {
return this.usersMapper.selectUserByUserid(userid);
}
@Override
public void updateUser(Users users) {
this.usersMapper.updateUser(users);
}
@Override
public void removeUser(int userid) {
this.usersMapper.delectUser(userid);
}
}
控制层
package com.bjsxt.controller;
import com.bjsxt.pojo.Users;
import com.bjsxt.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;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
/*用户*/
@RequestMapping("/addUsers")
public String addUsers(Users users){
this.usersService.addUser(users);
return "redirect:/users/findAll";
}
/*查询所有用户*/
@RequestMapping("/findAll")
public String findAll(Model model){
List<Users> list = this.usersService.findAll();
model.addAttribute("list",list);
return "show";
}
/*根据ID查询用户*/
@RequestMapping("/findById")
public String findById(Model model,int userid){
Users user = this.usersService.findUserById(userid);
model.addAttribute("user",user);
return "update";
}
/*更新用户*/
@RequestMapping("/updateUser")
public String updateUser(Model model,Users users){
this.usersService.updateUser(users);
return "redirect:/users/findAll";
}
/*删除用户*/
@RequestMapping("/delUser")
public String delUser(Model model,int userid){
this.usersService.removeUser(userid);
return "redirect:/users/findAll";
}
}
启动类
package com.bjsxt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.bjsxt.mapper") //@MapperScan 用户扫描MyBatis的Mapper
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
最新文章
- 面向对象(五)super
- Java继承中属性、方法和对象的关系
- UIImage imageNamed 与 imageWithContentsOfFile的差别
- python的深拷贝和浅拷贝
- 设计模式 - command
- rabbitMQ入门
- ios的标志常量
- FMCG行业是什么行业?
- c语言_头文件_stdlib
- linux基本命令2
- css3基本属性
- jstat命令详解
- 如何使用List<;HashMap<;String, String>;>;详细讲解
- 分表需要解决的问题 &; 基于MyBatis 的轻量分表落地方案
- hdu 1505,1506
- ItelliJ基于Gradle创建及发布Web项目(三)
- [2018湖南省队集训] 6.24 T1 marshland
- drf之视图
- Windows+Git+TortoiseGit+COPSSH安装图文教程【转】
- juniper ssg 常用命令
热门文章
- 转载]OK6410之tftp下载内核,nfs挂载文件系统全过程详解[转]
- php [poolwww] seemsbusy (youmayneedto increasepm.start_servers, or pm.min/max_spare_servers)错误解决方法
- 深入理解java多态没有烤山药的存在,java就不香了吗?
- golang 服务诡异499、504网络故障排查
- go中的数据结构通道-channel
- SQL的四种连接(左外连接、右外连接、内连接、全连接)
- HTML 空元素(转)
- Linux 7开机自启项查看并设置
- php之自动加载(懒加载)
- CentOS 7 Cobbler 配置 YUM仓库