使用Mybatis框架完成数据库中指定表的增删改查(查询一条数据和查询多条数据)操作
时间: 2024-03-15 21:43:56 浏览: 50
好的,我可以为您解答这个问题。首先需要说明的是,Mybatis是一种持久层框架,用于将Java对象映射到关系型数据库中的数据表。下面是使用Mybatis框架完成数据库中指定表的增删改查操作的示例代码。
1. 配置文件
Mybatis的配置文件包含了数据库连接信息、映射文件路径、插件等信息,下面是一个示例:
```
<?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/mapper/UserMapper.xml"/>
</mappers>
</configuration>
```
2. 映射文件
映射文件是Mybatis的核心,用于将Java对象映射到关系型数据库中的数据表,下面是一个示例:
```
<?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.mapper.UserMapper">
<resultMap id="UserResultMap" type="com.example.entity.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
<select id="getUserById" resultMap="UserResultMap">
SELECT * FROM user WHERE id = #{id}
</select>
<select id="getAllUsers" resultMap="UserResultMap">
SELECT * FROM user
</select>
<insert id="addUser" parameterType="com.example.entity.User">
INSERT INTO user (name, age) VALUES (#{name}, #{age})
</insert>
<update id="updateUser" parameterType="com.example.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代码,用于调用Mybatis完成数据库中指定表的增删改查操作:
```
public class UserMapperTest {
private SqlSession sqlSession;
private UserMapper userMapper;
@Before
public void setUp() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
userMapper = sqlSession.getMapper(UserMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testGetUserById() {
User user = userMapper.getUserById(1);
System.out.println(user);
}
@Test
public void testGetAllUsers() {
List<User> userList = userMapper.getAllUsers();
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void testAddUser() {
User user = new User();
user.setName("Tom");
user.setAge(25);
userMapper.addUser(user);
sqlSession.commit();
}
@Test
public void testUpdateUser() {
User user = new User();
user.setId(1);
user.setName("Tom");
user.setAge(30);
userMapper.updateUser(user);
sqlSession.commit();
}
@Test
public void testDeleteUser() {
userMapper.deleteUser(1);
sqlSession.commit();
}
}
```
以上就是使用Mybatis框架完成数据库中指定表的增删改查(查询一条数据和查询多条数据)操作的示例代码。
阅读全文