您好,登錄后才能下訂單哦!
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();
}
}
/**
* 根據ID查詢學生
*/
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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。