一:配置映射文件
<?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="StudentMapper" >
<select id="selectAll" resultType="com.hanjie.bean.Student">
select * from student;
</select>
<select id="selectById" resultType="com.hanjie.bean.Student" parameterType="java.lang.Integer">
select * from student where sid = #{id}
</select>
<insert id="insert" parameterType="com.hanjie.bean.Student">
insert into student values (#{sid},#{name},#{age},#{birthday})
</insert>
<update id="update" parameterType="com.hanjie.bean.Student">
update student set name =#{name},age=#{age} where sid = #{sid}
</update>
<delete id="delete" parameterType="java.lang.Integer">
delete from student where sid = #{#sid}
</delete>
</mapper>
二:使用
@Test
public void delete() throws Exception{
InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
int res = sqlSession.update("StudentMapper.delete", 100);
sqlSession.commit();//提交事务
System.out.println(res);
sqlSession.close();
is.close();
}
@Test
public void update() throws Exception{
InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
Student student = new Student(100,"hanjie",100,"2021-01-01");
int res = sqlSession.update("StudentMapper.update", student);
sqlSession.commit();//提交事务
System.out.println(res);
sqlSession.close();
is.close();
}
@Test
public void insert() throws Exception{
InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
Student student = new Student(100,"hanjie",26,"2021-01-01");
int res = sqlSession.insert("StudentMapper.insert", student);
sqlSession.commit();//提交事务
System.out.println(res);
sqlSession.close();
is.close();
}
@Test
public void selectById() throws Exception{
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
Student student = sqlSession.selectOne("StudentMapper.selectById", 3);
System.out.println(student);
sqlSession.close();
is.close();
}
@Test
public void selectAll() throws Exception{
//1.加载配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过SqlSession工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.执行配置文件中sql语句,并接受结果
List<Student> students = sqlSession.selectList("StudentMapper.selectAll");
//5.处理结果集
for (Student student:students) {
System.out.println(student);
}
//6.释放资源
sqlSession.close();
is.close();
}