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>