使用MyBatis对数据库进行增删改查
时间: 2023-12-28 14:03:34 浏览: 89
MyBatis是一种持久层框架,它可以帮助我们轻松地实现数据库操作。下面是使用MyBatis进行增删改查的基本步骤:
1. 配置MyBatis的XML文件(mybatis-config.xml)和映射文件(mapper.xml)。
2. 创建数据库连接,并将连接信息配置到mybatis-config.xml文件中。
3. 在mapper.xml文件中定义SQL语句,包括select、insert、update和delete等操作。
4. 在Java代码中使用SqlSessionFactoryBuilder构建SqlSessionFactory实例,并通过SqlSession获取Mapper实例。
5. 在Java代码中调用Mapper实例的方法执行SQL语句,完成增删改查操作。
下面是一个简单的示例代码:
1. 配置文件mybatis-config.xml
```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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/demo/mapper/UserMapper.xml"/>
</mappers>
</configuration>
```
2. 映射文件UserMapper.xml
```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.example.demo.mapper.UserMapper">
<resultMap id="userMap" type="com.example.demo.entity.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
<select id="getUserById" parameterType="int" resultMap="userMap">
select id, name, age from user where id = #{id}
</select>
<select id="getAllUsers" resultMap="userMap">
select id, name, age from user
</select>
<insert id="insertUser" parameterType="com.example.demo.entity.User">
insert into user (name, age) values (#{name}, #{age})
</insert>
<update id="updateUser" parameterType="com.example.demo.entity.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>
```
3. Java代码
```java
public class UserMapperTest {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testGetUserById() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
} finally {
sqlSession.close();
}
}
@Test
public void testGetAllUsers() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getAllUsers();
System.out.println(userList);
} finally {
sqlSession.close();
}
}
@Test
public void testInsertUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User("张三", 20);
userMapper.insertUser(user);
sqlSession.commit();
System.out.println(user.getId());
} finally {
sqlSession.close();
}
}
@Test
public void testUpdateUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User(1, "李四", 25);
userMapper.updateUser(user);
sqlSession.commit();
} finally {
sqlSession.close();
}
}
@Test
public void testDeleteUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(1);
sqlSession.commit();
} finally {
sqlSession.close();
}
}
}
```
以上代码演示了通过MyBatis对数据库进行增删改查的基本操作。
阅读全文