基于上一章节继续
1.修改userMapper.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">
<!-- namespace可以随便取名,但是一般都是包名+配置文件名 -->
<mapper namespace="com.mybatis.userMapper">
<!-- 查询用户信息 -->
<select id="selectUserById" parameterType="int" resultType="User">
select * from users where id = #{id}
</select>
<!-- 查询全部用户信息 -->
<select id="selectAllUser" resultType="User">
select * from users
</select>
<!-- 新增用户信息 -->
<insert id="insertUser" parameterType="User">
insert into users(ID,NAME,AGE,ADDRESS) values(#{id},#{name},#{age},#{address})
</insert>
<!-- 删除用户信息 -->
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<!-- 更新用户信息 -->
<update id="updateUser" parameterType="User">
update users set address=#{address} where id=#{id}
</update>
</mapper>
2.修改TestMybatis.java中的内容
package com.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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.Test;
import com.entity.User;
public class TestMybatis {
private static SqlSessionFactory sqlSessionFactory;
private static SqlSession sqlSession;
public static SqlSession getSession() throws IOException {
//这里读取全局mybatis配置文件
String resource="mybatis-config.xml";
Reader reader=Resources.getResourceAsReader(resource);
sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
//查询用户
@Test
public void selectUser() throws IOException {
/**
* 这里的变量state就是userMapper.xml配置文件中的namespace+对应的id
*/
String state="com.mybatis.userMapper.selectUserById";
//根据id为1002的值去查询用户
User user=(User)getSession().selectOne(state, 1002);
System.out.println(user);
sqlSession.close();
}
//查询全部用户信息
@Test
public void selectAllUser() throws IOException {
String state="com.mybatis.userMapper.selectAllUser";
List<User> user=getSession().selectList(state);
sqlSession.close();
System.out.println(user);
}
//新增用户信息
@Test
public void insertUser() throws IOException {
String state="com.mybatis.userMapper.insertUser";
User user=new User();
user.setId(1003);
user.setName("fengjie");
user.setAge(29);
user.setAddress("China");
int result=getSession().insert(state, user);
//事务提交
sqlSession.commit();
//释放资源
sqlSession.close();
//操作成功就会返回1
System.out.println(result);
}
//修改用户信息
@Test
public void updateUser() throws IOException {
String state="com.mybatis.userMapper.updateUser";
User user=new User();
user.setId(1001);
user.setAddress("China");
//根据id的值去修改address
int result=getSession().update(state, user);
sqlSession.commit();
sqlSession.close();
//操作成功就会返回1
System.out.println(result);
}
//删除用户信息
@Test
public void deleteUser() throws IOException {
String state="com.mybatis.userMapper.deleteUser";
int result=getSession().delete(state, 1008);
sqlSession.commit();
sqlSession.close();
//操作成功就会返回1
System.out.println(result);
}
}
新增用户结果
查询全部用户