目录
源码下载:https://download.csdn.net/download/adam_zs/24945109
1.linux安装mysql8.0
参考:https://blog.csdn.net/young_0609/article/details/109015666
![](https://i-blog.csdnimg.cn/blog_migrate/00d28d391ac04e31ed80d7d464e38132.png)
2.安装mysql客户端Navicat Premium 12
3.spring boot 集成mybatis,mysql
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.5.5</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzs.springboot</groupId>
<artifactId>springboot.mybatis</artifactId>
<version>1.0.0</version>
<name>spring-boot-mybatis-mysql</name>
<description>Spring Boot Mybatis Mysql</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</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>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8080 #tomcat端口
spring: #springboot的配置
datasource: #定义数据源
name: mysqlTest #定义数据源的名字
#10.18.22.10 服务器地址,mysql数据库名字
url: jdbc:mysql://10.18.xx.xx:3306/wzsmysql?serverTimezone=GMT%2B8&useSSL=FALSE
username: root #数据库用户名,root为管理员
password: 123456 #该数据库用户的密码
mybatis: #mybatyis的配置
mapper-locations: classpath:mapper/*.xml #指定mapper的配置文件的路径是mapper文件夹下的所有xml文件。
建表
CREATE TABLE user(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT '学号',
name VARCHAR(20) COMMENT '姓名',
age int COMMENT '年龄'
) COMMENT='学生信息';
select * from user;
insert user(id,name,age) VALUES(null,'wangzs1',31);
insert user(id,name,age) VALUES(null,'wangzs2',32);
insert user(id,name,age) VALUES(null,'wangzs3',33);
insert user(id,name,age) VALUES(null,'wangzs4',34);
package com.wzs.springboot.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wzs.springboot.mybatis")
public class SpringBootMybatisMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisMysqlApplication.class, args);
}
}
package com.wzs.springboot.mybatis;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
}
package com.wzs.springboot.mybatis;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserServiceImpl userService;
@RequestMapping("/findAll")
public String findAll() {
List<User> users = userService.findAll();
return users.toString();
}
@RequestMapping("/add")
public int addUser() {
User u = new User(null, "wangzs-" + System.currentTimeMillis(), 45);
int stat = userService.addUser(u);
return stat;
}
}
package com.wzs.springboot.mybatis;
import java.util.List;
public interface UserMapper {
int insertUser(User user);
int deleteUser(Integer id);
User selectUser(Integer id);
int updateUser(User user);
List<User> selectAll();
}
package com.wzs.springboot.mybatis;
import java.util.List;
public interface UserService {
int addUser(User user);
int deleteUser(Integer id);
User selectUser(Integer id);
int updateUser(User user);
List<User> findAll();
}
package com.wzs.springboot.mybatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.insertUser(user);
}
@Override
public int deleteUser(Integer id) {
return userMapper.deleteUser(id);
}
@Override
public User selectUser(Integer id) {
return userMapper.selectUser(id);
}
@Override
public List<User> findAll() {
return userMapper.selectAll();
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--上面2行的是约束依赖,固定照抄就好 -->
<!--下面的才是要自己编写的地方 -->
<!--写mapper的配置文件第一步就是要写<mapper></mapper>标签 -->
<!--<mapper></mapper>标签里包含着各个CURD操作的SQL语句 -->
<mapper namespace="com.wzs.springboot.mybatis.UserMapper">
<!--定义一个名为BaseResultMap的返回类型 -->
<resultMap id="BaseResultMap" type="com.wzs.springboot.mybatis.User">
<id column="id" property="id" jdbcType="INTEGER"></id>
<result column="name" property="name" jdbcType="VARCHAR"></result>
<result column="age" property="age" jdbcType="INTEGER"></result>
</resultMap>
<!--插入语句 -->
<!--id要与UserMapper文件中表示插入的函数名一致,parameterType表示函数的输入参数的类型 -->
<insert id="insertUser" parameterType="com.wzs.springboot.mybatis.User">
insert into
user(id,name,age)values(#{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},#{age,jdbcType=INTEGER})/*SQL语句*/
</insert>
<!--删除语句 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id =#{id,jdbcType=INTEGER}
</delete>
<!--查找语句 -->
<!--resultMap表示函数返回的类型 -->
<select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from user where id =
#{id,jdbcType=INTEGER}
</select>
<!--查找语句 -->
<select id="selectAll" resultMap="BaseResultMap">
select * from user
</select>
<!--修改语句 -->
<update id="updateUser" parameterType="com.wzs.springboot.mybatis.User">
update user set
name=#{name,jdbcType=VARCHAR},age=#{age,jdbcType=INTEGER}
where id=#{id,jdbcType=INTEGER}
</update>
</mapper>
4.测试
package com.wzs.springboot.mybatis;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringBootMybatisMysqlApplicationTests {
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private UserServiceImpl userService;
@Before
public void setUp() throws MalformedURLException {
String url = String.format("http://localhost:%d/", port);
System.out.println(String.format("##SpringBootMybatisMysqlApplicationTests port is : [%d]", port));
this.base = new URL(url);
}
@Test
public void findAll_test() {
ResponseEntity<String> response = this.restTemplate.getForEntity(this.base.toString() + "/user/findAll",
String.class);
System.out.println(response);
}
@Test
public void add_test() {
ResponseEntity<String> response = this.restTemplate.getForEntity(this.base.toString() + "/user/add",
String.class);
System.out.println(response);
}
@Test
public void selectUser_test() {
User user = userService.selectUser(11);
System.out.println(user);
}
@Test
public void updateUser_test() {
User user = userService.selectUser(11);
System.out.println(userService.selectUser(11));
user.setName("wangsm");
userService.updateUser(user);
System.out.println(userService.selectUser(11));
}
}