发布时间:2023-03-25 15:30
(1)在UserMapper.xml里增加映射语句
(2)在UserMapper接口里增加查询方法
(3)在测试程序TestUserMapper里增加一个测试方法
(3)在测试程序TestUserMapper里增加一个测试方法
(3)在测试程序TestUserMapper里增加一个测试方法
(3)在测试程序TestUserMapper里增加一个测试方法
(3)在测试程序TestUserMapper里增加一个测试方法
(3)在测试程序TestUserMapper里增加一个测试方法
<mapper namespace="net.cw.mybatis.mapper.UserMapper">
<resultMap id="userMap" type="User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="address" property="address"/>
resultMap>
<select id="findById" parameterType="int" resultType="User">
SELECT * FROM user WHERE id = #{id};
select>
<select id="findAll" resultMap="userMap">
SELECT * FROM user;
select>
<select id="findByName" resultType="User">
SELECT * FROM user WHERE name LIKE concat("%",#{name},"%");
select>
<update id="update" parameterType="User">
UPDATE user SET name = #{name}, age = #{age},
address = #{address} WHERE id = #{id};
update>
<insert id="insert" parameterType="User"
useGeneratedKeys="true" keyProperty="id">
INSERT INTO user(name, age, address)
VALUES(#{name}, #{age}, #{address});
insert>
<delete id="deleteById" parameterType="int">
DELETE FROM user WHERE id = #{id};
delete>
mapper>
package net.cw.mybatis.mapper;
import net.cw.mybatis.bean.User;
import java.util.List;
/**
* 功能:用户映射器接口
* 作者:cw
* 日期:2020.9.14
*/
public interface UserMapper {
User findById(int id);
List<User> findAll();
List<User> findByName(String name);
int insert(User user);
int update(User user);
int deleteById(int id);
}
package net.cw.mybatis.mapper;
import net.cw.mybatis.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class TestUserMapper {
private SqlSession session;
private UserMapper userMapper;
@Before
public void init() {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
session = factory.openSession();
userMapper = session.getMapper(UserMapper.class);
System.out.println("session对象已创建。");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testFindById() {
int id = 1;
User user = userMapper.findById(id);
System.out.println(user);
}
@Test
public void testFindAll() {
List<User> users = userMapper.findAll();
for (User user : users) {
System.out.println(user);
}
}
@Test
public void testFindByName() {
String name = "王";
List<User> users = userMapper.findByName(name);
for (User user : users) {
System.out.println(user);
}
}
@Test
public void testInsert() {
User user = new User();
user.setName("李晓雨");
user.setAge(30);
user.setAddress("古蔺县郎酒厂");
int count = userMapper.insert(user);
session.commit(); // 提交数据库操作
if (count > 0) {
System.out.println("记录插入成功!");
System.out.println("插入记录:" + user);
} else {
System.out.println("记录插入失败!");
}
}
@Test
public void testUpdate() {
User user = userMapper.findById(4);
user.setName("萌萌哒");
user.setAge(30);
user.setAddress("北京朝阳区");
userMapper.update(user);
session.commit();
System.out.println("更新成功!");
System.out.println("更新记录:" + userMapper.findById(4));
}
@Test
public void testDeleteById() {
userMapper.deleteById(4);
session.commit();
System.out.println("记录删除成功!");
}
@After
public void destroy() {
session.close();
System.out.println("session对象已关闭。");
}
}