Spring5-整合-Mybatis
环境搭建
步骤:
1,导入相关jar包
- Junit
- mybatis
- mysql数据库
- spring相关的
- aop植入
- Mybatis-spring【new 知识点】 https://mybatis.org/spring/zh/index.html
2,编写配置文件
3,测试
1,创建Maven项目
在pom.xml中导入jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
为了建立子项目,把src删掉
new 一个Module
2,在pom.xml导入包
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring操作数据库的话,还需要一个spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--aop植入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--mybatis-spring 整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
回忆mybayis
1,编写实体类
- 连接数据库
User.java
package com.study.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
2,编写核心配置文件
-
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration核心配置文件--> <configuration> <!--第一步:给实体类起别名--> <typeAliases> <package name="com.study.pojo"/> </typeAliases> <!--第二步连接数据库--> <!--environments: 环境--> <environments default="development"> <environment id="development"> <!--transactionManager:事务管理--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="111111"/> </dataSource> </environment> </environments> </configuration>
3,编写接口
-
UserMapper
package com.study.mapper; import com.study.pojo.User; import java.util.List; //mapper:是一个接口 public interface UserMapper { public List<User> selectUser(); }
4,编写Mapper.xml
-
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace:绑定接口--> <mapper namespace="com.study.mapper.UserMappper"> <select id="selectUser" resultType="user"> select * from mybatis.user </select> </mapper>
5,测试
-
MyTest
import com.study.mapper.UserMappper; import com.study.pojo.User