mybatis(2)增刪改查

mybatis.xml,db.properties,MybatisUtil.java,student.java和第一篇文章一樣,所建的表也是一樣的

StudentDao1.java

/**
 * 持久層 
 * @author AdminTC
 */
public class StudentDao1 {
	/**
	 * 增加學生
	 */
	public void add(Student student) throws Exception{
		SqlSession sqlSession = null;
		try{
			sqlSession = MybatisUtil.getSqlSession();
			sqlSession.insert(Student.class.getName()+".add",student);
			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{
		StudentDao1 dao = new StudentDao1();
		//dao.add(new Student(1,"哈哈",7000D));
		//dao.add(new Student(2,"呵呵",8000D));
		//dao.add(new Student(3,"班長",9000D));
		//dao.add(new Student(4,"鍵狀高",10000D));
		//Student student = dao.findById(4);
		//List<Student> studentList = dao.findAll();
		//for(Student student : studentList){
		//	System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());
		//	System.out.println();
		//}
		//Student student = dao.findById(3);
		//student.setName("靚班長");
		//dao.update(student);
		
		Student student = dao.findById(3);
		System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());
		
		//dao.delete(student);
	}
}
StudentMapper.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">

<mapper namespace="com.bmw.javaee.mybatis.app09.Student">	

	
	<resultMap type="com.bmw.javaee.mybatis.app09.Student" id="studentMap">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sal" column="sal"/>
	</resultMap>




	<!-- 增加學生 -->
	<insert id="add" parameterType="com.bmw.javaee.mybatis.app09.Student">
		insert into students(id,name,sal) values(#{id},#{name},#{sal})
	</insert>
	
	
	
	<!-- 根據ID查詢學生
		 如果參數不是一個實體的話,只是一個普通變量,例如:int,double,String
		 這裏的#{中間的變量名可以隨便寫},不過提倡就用方法的形參
	 -->
	<select id="findById" parameterType="int" resultType="com.bmw.javaee.mybatis.app09.Student">
		select id,name,sal from students where id = #{id}
	</select>
	
	<!-- 查詢所有學生 
		 理論上resultType要寫List<Student>
		 但這裏只需書寫List中的類型即可,即只需書寫Student的全路徑名
	-->
	<select id="findAll" resultType="com.bmw.javaee.mybatis.app09.Student">
		select id,name,sal from students
	</select>
	
	
	
	<!-- 更新學生 -->
	<update id="update" parameterType="com.bmw.javaee.mybatis.app09.Student">
		update students set name=#{name},sal=#{sal} where id=#{id}
	</update>
	
	
	
	<!-- 刪除學生 --> 
	<delete id="delete" parameterType="com.bmw.javaee.mybatis.app09.Student">
		delete from students where id = #{id}
	</delete>
	

	<!-- 
	<insert id="delete" parameterType="com.bmw.javaee.mybatis.app09.Student">
		delete from students where id = #{id}
	</insert>
	-->	
	
	
	<!-- 
		注意:這個insert/update/delete標籤只是一個模板,在做操作時,其實是以SQL語句爲核心的
		     即在做增/刪/時,insert/update/delete標籤可通用,
		     但做查詢時只能用select標籤
		     我們提倡什麼操作就用什麼標籤
	-->	
	
</mapper>
StudentDao2.java

/**
 * 持久層 
 * @author AdminTC
 */
public class StudentDao2 {
	/**
	 * 增加學生
	 */
	public void add(Student student) throws Exception{
		SqlSession sqlSession = null;
		try{
			sqlSession = MybatisUtil.getSqlSession();
			sqlSession.insert("studentNamespace.add",student);
			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("studentNamespace.findById",id);
			sqlSession.commit();
			return student;
		}catch(Exception e){
			e.printStackTrace();
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
	}

	public static void main(String[] args) throws Exception{
		StudentDao2 dao = new StudentDao2();
		//dao.add(new Student(1,"班長",7000D));
		Student student = dao.findById(1);
		if(student == null){
			System.out.println("YES");
		}
		System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
	}
}
StudentMapper2.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">

<mapper namespace="studentNamespace">	

	<!-- 當實體屬性與表字段名不相同的時候,必須書寫以下代碼
	     當實體屬性與表字段名相同的時候,以下代碼可選 
	 -->
	<resultMap type="com.bmw.javaee.mybatis.app09.Student" id="studentMap">
		<id property="id" column="students_id"/>
		<result property="name" column="students_name"/>
		<result property="sal" column="students_sal"/>
	</resultMap>
	
	
	
	
	<!-- 增加學生 -->
	<insert id="add" parameterType="com.bmw.javaee.mybatis.app09.Student">
		insert into students(students_id,students_name,students_sal) 
		values(#{id},#{name},#{sal})
	</insert>

	
	
	<!-- 根據ID查詢學生 
		 mybatis會將查詢出來的表記錄和studentMap這個id所對應的映射結果相互匹配
	-->
	<select id="findById" parameterType="int" resultMap="studentMap">
		select students_id,students_name,students_sal
		from students
		where students_id = #{id}
	</select>
	
	
</mapper>



發佈了45 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章