-
编写接口
-
编写对应的Mapper.xml中的sql语句
-
测试(增删改需要提交事务)
<mapper namespace="com.perwrj.dao.UserMapper">
<select id="getUserList" resultType="com.perwrj.pojo.User">
select * from mybatis.user
</select>
</mapper>
-
id:就是对应的namespace中的方法名
-
resultType:sql语句的返回值类型
-
parameterType:传入参数的类型
package com.perwrj.dao;
import java.util.List;
import com.perwrj.pojo.User;
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
<?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">
<!-- namespace绑定一个对应的Mapper/Dao接口 -->
<mapper namespace="com.perwrj.dao.UserMapper">
<!-- 查询 id=“重写的方法名” resultType=“返回值类型(全类名)” -->
<select id="getUserList" resultType="com.perwrj.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="com.perwrj.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<!-- 对象中的属性可以直接取出来 -->
<insert id="addUser" parameterType="com.perwrj.pojo.User">
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="com.perwrj.pojo.User">
update mybatis.user set name = #{name},pwd = #{pwd} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
package com.perwrj.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.perwrj.pojo.User;
import com.perwrj.utils.MyBatisUtil;
public class UserMapperTest {
@Test
public void test() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User>
userList = userMapper.getUserList();
for (User user : userList) { System.out.println(user); }
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void deleteUserTest() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int num = userMapper.deleteUser(4);
if(num > 0) {
System.out.println(num);
sqlSession.commit();
sqlSession.close();
}
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void updateUserTest() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int num = userMapper.updateUser(new User(2,"杰瑞","987654"));
if(num > 0) {
System.out.println(num);
sqlSession.commit();
sqlSession.close();
}
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void addUserTest() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int num = userMapper.addUser(new User(4,"王瑞杰","123568"));
if(num > 0) {
System.out.println(num);
sqlSession.commit();
sqlSession.close();
}
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void testGetUserById() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
来源:https://www.cnblogs.com/wangruijie/p/14449332.html