一、前言
最近在跟着视频做码匠社区项目,视频中连接H2数据库,但是我这个社区版的IDEA无法集成H2,暂时还未找到解决办法。
因此想到了之前练习的连接MySQL,为了能继续下去,只能先这样了,以后找到解决办法再改成H2吧!
二、项目架构
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.2.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>life.majiang.community</groupId>
<artifactId>community</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>community</name>
<description>community</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</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>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、springboot集成MySQL
如果不做更改,test connection不会成功。
四、注解版实现增删改查
我习惯从controller开写
package life.majiang.community.controller;
import life.majiang.community.bean.User;
import life.majiang.community.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController {
@Autowired
UserMapper userMapper;
@Autowired
User user;
@GetMapping("/getUserById")
public String getUserById(@RequestParam(name="id") String id){
System.out.println(userMapper.getUserById(id).name);
return "index";
}
@GetMapping("/getUsers")
public String getUsers(){
return "index";
}
@GetMapping("/insertUser")
public String insertUser(@RequestParam(name="id") String id,@RequestParam(name="name") String name,@RequestParam(name="password") String password){
user.setId(id);
user.setName(name);
user.setPassword(password);
userMapper.insertUser(user);
return "index";
}
}
UserMapper
package life.majiang.community.mapper;
import life.majiang.community.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("select * from user where id=#{id}")
public User getUserById(String id);
@Insert("insert into user(ID,name,password) values (#{id},#{name},#{password})")
public void insertUser(User user);
@Select("select * from user")
public User getUsers();
}
测试
五、配置实现增删改查
package life.majiang.community.controller;
import life.majiang.community.bean.User;
import life.majiang.community.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController {
@Autowired
UserMapper userMapper;
@Autowired
User user;
@GetMapping("/getUserById2")
public String getUserById2(@RequestParam(name="id") String id){
System.out.println(userMapper.getUserById2(id).name);
return "index";
}
@GetMapping("/insertUser2")
public String insertUser2(@RequestParam(name="id") String id,@RequestParam(name="name") String name,@RequestParam(name="password") String password){
user.setId(id);
user.setName(name);
user.setPassword(password);
userMapper.insertUser2(user);
return "index";
}
@GetMapping("/deleteUserById")
public String deleteUserById(@RequestParam(name="id") String id){
userMapper.deleteUserById(id);
return "index";
}
}
UserMapper
package life.majiang.community.mapper;
import life.majiang.community.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
public User getUserById2(String id);
public void insertUser2(User user);
public void deleteUserById(String id);
}
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="life.majiang.community.mapper.UserMapper">
<select id="getUserById2" resultType="life.majiang.community.bean.User">
SELECT * FROM user WHERE id=#{id};
</select>
<insert id="insertUser2">
insert into user(ID,name,password) values (#{id},#{name},#{password});
</insert>
<delete id="deleteUserById">
delete from user where id=#{id};
</delete>
</mapper>
application.yml
测试
六、总结
springboot通过mybatis实现增删改查还是挺简单的,但也有很多问题需要注意,比如@MapperScan,application.yml的配置,驼峰的设置等等。