0、写在前面
此文为记录非逆向工程,导入数据库
- SQLyog新建数据库
- IDEA新建project
- 配置POM.xml
- 配置application.properties
- 连接数据库
- pojo层
- mapper层
- resource.mybatis.mapper:xml
- controller层
- 启动测试
1、SQLyog新建数据库
2、IDEA新建project
选web和SQL
目录如下:
3、配置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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.szu</groupId>
<artifactId>springboot-06-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-06-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--新添加的依赖-->
<!--Mybatis集成SpringBoot框架起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--以下官方的-->
<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>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>
<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>
4、配置application.properties
完整代码如下:
#连接数据库的四要素
spring.datasource.username=cwq1
spring.datasource.password=chen
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
测试一下:
添加代码如下:
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
run 一下 contextLoads(),测试成功
5、连接数据库
选择 Datasource -> MySQL
填写用户和密码,点击测试连接
如果报错:点击Set time zone
选择所需的schema
再测试一次,成功
6、pojo层
新建pojo层
添加代码如下:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
7、mapper层
新建mapper层
添加代码如下:
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
8、在resource里写UserMapper.xml
新建mybatis文件夹,进去再新建mapper文件夹,进去再新建file,填写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.szu.mapper.UserMapper">
<!--对应着UserMapper接口-->
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" resultType="User">
select * from user where id = #{id}
</select>
<select id="addUser" resultType="User">
select into user (id,name,age) values (#{id},#{name},#{age})
</select>
<update id="updateUser" parameterType="User">
update user set name=#{name},age=#{age} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
为了使得该xml可以被识别到,需要去application.properties整合mybatis
添加代码如下:
#整合mybatis
mybatis.type-aliases-package=com.szu.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
9、controller层
同样套路,新建、添加
添加的代码如下:
@RestController
public class UserController {
@Autowired private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList() {
List<User> userList = userMapper.queryUserList();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
}
10、启动测试
去浏览器测试一下: