搭建mybatis框架,采用测试文件测试添加,删除,修改,更新,模糊查询的方法
时间: 2024-05-16 12:15:58 浏览: 53
以下是搭建MyBatis框架并进行基本操作的示例代码:
1. 首先,需要在maven中添加MyBatis和数据库驱动的依赖:
```xml
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
```
2. 创建数据库表和对应的Java实体类,并在MyBatis配置文件中添加相关配置:
```xml
<!-- 数据库连接配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 映射器配置 -->
<mappers>
<mapper resource="mapper/UserMapper.xml" />
</mappers>
```
3. 创建映射器接口和对应的映射器XML文件,并在XML文件中编写对应的SQL语句:
UserMapper.java
```java
public interface UserMapper {
// 添加用户
void addUser(User user);
// 根据ID删除用户
void deleteUserById(Integer id);
// 修改用户
void updateUser(User user);
// 根据ID查询用户
User getUserById(Integer id);
// 模糊查询用户
List<User> getUserByKeyword(String keyword);
}
```
UserMapper.xml
```xml
<mapper namespace="com.example.mapper.UserMapper">
<!-- 添加用户 -->
<insert id="addUser" parameterType="com.example.entity.User">
insert into user(name, age, gender) values (#{name}, #{age}, #{gender})
</insert>
<!-- 根据ID删除用户 -->
<delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
<!-- 修改用户 -->
<update id="updateUser" parameterType="com.example.entity.User">
update user set name = #{name}, age = #{age}, gender = #{gender} where id = #{id}
</update>
<!-- 根据ID查询用户 -->
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.entity.User">
select * from user where id = #{id}
</select>
<!-- 模糊查询用户 -->
<select id="getUserByKeyword" parameterType="java.lang.String" resultType="com.example.entity.User">
select * from user where name like concat('%', #{keyword}, '%') or gender like concat('%', #{keyword}, '%')
</select>
</mapper>
```
4. 创建测试类,并使用SqlSessionFactory和SqlSession获取Mapper接口实例进行数据库操作:
```java
public class UserMapperTest {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void setUp() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testAddUser() {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
User user = new User("Tom", 18, "male");
mapper.addUser(user);
session.commit();
}
}
@Test
public void testDeleteUserById() {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
mapper.deleteUserById(1);
session.commit();
}
}
@Test
public void testUpdateUser() {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
User user = new User(2, "Lucy", 20, "female");
mapper.updateUser(user);
session.commit();
}
}
@Test
public void testGetUserById() {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.getUserById(2);
System.out.println(user);
}
}
@Test
public void testGetUserByKeyword() {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> userList = mapper.getUserByKeyword("m");
for (User user : userList) {
System.out.println(user);
}
}
}
}
```
这样,就可以进行基本的增删改查和模糊查询操作了。
阅读全文