1.创建springBoot项目
目录结构如下,从第二步开始做起,不要直接全创了
2.导入pom依赖
创建项目的时候导入Web,Log4j,JDBC,Mysql驱动,等依赖
<?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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot_mybatis</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--Durid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!--log4j-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--JDBC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mysql-->
<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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.resources下创建application.properties
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#ÕûºÏmybatis
mybatis.type-aliases-package=com.springboot_mybatis.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
4.在test中测试能否连接成功
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
5.测试成功后,创建实体类User和Usermapper接口
User.class
@Component
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
接口UserMapepr.class
@Mapper
@Repository
public interface UserMapper {
//获取所有员工的信息
List<User> queryUserList();
//通过id查询一个员工
User queryUserById(int id);
//新增一个员工
int addUser(User user);
//修改一个员工
int updateUser(User user);
//通过id删除员工信息
int deleteUser(int id);
}
6.UserMapper接口的.xml文件
resource/mapper/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.springboot_mybatis.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user;
</select>
<select id="queryUserById" resultType="User">
select * from user where id=#{id};
</select>
<insert id="addUser" parameterType="User">
insert into user(username,password) values (#{username},#{password});
</insert>
<update id="updateUser" parameterType="User">
update user set username=#{username},password=#{password} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id =#{id};
</delete>
</mapper>
7.我这里直接创建Controller,可以先创建一个Service进行封装,然后用Controller去调
controller/UserController.class
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryUserList")
public List<User> queryUserList() {
List<User> userList = userMapper.queryUserList();
return userList;
}
@RequestMapping("/queryUserById/{id}")
public User queryUserById(@PathVariable("id") Integer id) {
User user = userMapper.queryUserById(id);
return user;
}
@RequestMapping("/addUser")
public String addUser() {
User user = new User();
user.setUsername("五毛");
user.setPassword("5555555");
userMapper.addUser(user);
return "add_ok";
}
@RequestMapping("/updateUser")
public String updateUser() {
User user = new User(8, "五毛", "6666");
userMapper.updateUser(user);
return "update_ok";
}
@RequestMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") Integer id) {
userMapper.deleteUser(id);
return "delete_ok";
}
}
8.启动主启动类进行测试,访问http://localhost:8080/+路径
StudentApplication.class
@SpringBootApplication
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}
}