import java.util.*;
import cn.mybatis.entity.Student;
import cn.mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
public class StudentDao {
public void add(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
//事务开始(默认)
//读取StudentMapper.xml映射文件中的SQL语句
int i = sqlSession.insert(Student.class.getName()+".add",student);
System.out.println("本次操作影响了"+i+"行");
//事务提交
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
//事务回滚
sqlSession.rollback();
throw e;
}finally{
//MybatisUtil.closeSqlSession();
}
}
public Student findById(int id) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
Student student = sqlSession.selectOne(Student.class.getName()+".findById",id);
sqlSession.commit();
return student;
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
public List<Student> findAll() throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectList(Student.class.getName()+".findAll");
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
public void update(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.update(Student.class.getName()+".update",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
public void delete(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete(Student.class.getName()+".delete",student);
//事务
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
// 回滚
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception {
StudentDao dao = new StudentDao();
// dao.add(new Student(3,"美丽",70030.3));
// dao.add(new Student(4,"加油",70030.3));
// dao.add(new Student(5,"关系",70030.3));
// dao.add(new Student(6,"规律",70030.3));
// dao.add(new Student(7,"古蔺",70030.3));
// List<Student> studentslist = dao.findAll();
// for (Student student : studentslist ) {
// System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());
// }
// Student student = dao.findById(4);
// student.setName("liwen");
// dao.update(student);
}
}
<?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="cn.mybatis.entity.Student">
<resultMap type="cn.mybatis.entity.Student" id="studentMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap>
<!-- 增加学生 -->
<insert id="add" parameterType="cn.mybatis.entity.Student">
insert into students(id,name,sal) values(#{id},#{name},#{sal})
</insert>
<!-- 根据ID查询学生
如果参数不是一个实体的话,只是一个普通变量,例如:int,double,String
这里的#{中间的变量名可以随便写},不过提倡就用方法的形参
-->
<select id="findById" parameterType="int" resultType="cn.mybatis.entity.Student">
select id,name,sal from students where id = #{id}
</select>
<!-- 查询所有学生
理论上resultType要写List<Student>
但这里只需书写List中的类型即可,即只需书写Student的全路径名
-->
<select id="findAll" resultType="cn.mybatis.entity.Student">
select id,name,sal from students
</select>
<!-- 更新学生 -->
<update id="update" parameterType="cn.mybatis.entity.Student">
update students set name=#{name},sal=#{sal} where id=#{id}
</update>
<!-- 删除学生 -->
<delete id="delete" parameterType="cn.mybatis.entity.Student">
delete from students where id = #{id}
</delete>
<!-- 无条件分页 -->
<select id="findAllWithFy" parameterType="map" resultMap="studentMap">
select id,name,sal from students limit #{pstart},#{psize}
</select>
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341