spring3+mbatis3开发实例
最近一直在深入了解struts2,spring,hibernate以及mybatis框架,通过查看这些框架的源码和官方文档,发现自己对于这些框架的原理,使用有了更深的理解,那么今天
我给大家带来的是运用spring和mybatis这两个框架来开发的小例子,并给大家讲述一些开发中需要注意的一些细节。
1、新建一个web项目,修改web.xml文件,我的文件内容如下,大家把需要的拷走就行:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>dreamMall-dubbo-provider</display-name>
<!-- 加载spring文件监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 解决spring容器运行时可能产生的内存溢出 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- spring文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- log4j文件路径 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 开启watchdog线程监测配置文件的变化 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- 设置编码格式 -->
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>
private static final long serialVersionUID = 1L;
private int id;
private String courceName;
private int teacherId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
public String getCourceName() {
return courceName;
}
public void setCourceName(String courceName) {
this.courceName = courceName;
}
@Override
public String toString() {
return "Cource [id=" + id + ", courceName=" + courceName + "]";
}
}
student.java:
package com.mall.dubbo.entity;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private int age;
private String studentName;
private int sex;
private int teacherId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", studentName="
+ studentName + ", sex=" + sex + "]";
}
}
Teacher.java:
package com.mall.dubbo.entity;
import java.io.Serializable;
import java.util.List;
public class Teacher implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String teacherName;
private int sex;
private int age;
private Cource cource;
private List<Student> students;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cource getCource() {
return cource;
}
public void setCource(Cource cource) {
this.cource = cource;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", teacherName=" + teacherName + ", sex="
+ sex + ", age=" + age + "]";
}
}
4、建立映射关系,这部分是最重要的,这里面的知识点,我希望大家可以好好看,如果有什么问题的,可以评论留言,或者去查看mybatis3.2的官方文档
地址是 http://wenku.baidu.com/link?url=L6Lu0GufwrMCgBLGUbsfGy7Os6s7MEcKIsZQj7JhOxIo6BSbsULynqsWeqX0mIyIqkzLIozaQvnaAUROrWypUDQj3QBfe5j6jO3solfO3-G
cource.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.mall.dubbo.dao.CourceDao">
<resultMap type="com.mall.dubbo.entity.Cource" id="courceMap">
<id property="id" column="id" javaType="int" jdbcType="INTEGER"/>
<result property="courceName" column="cource_name" javaType="String" jdbcType="VARCHAR"/>
<result property="teacherId" column="teacher_id" javaType="int" jdbcType="INTEGER"/>
</resultMap>
<parameterMap type="com.mall.dubbo.entity.Cource" id="paraMap">
<parameter property="id"/>
<parameter property="courceName"/>
<parameter property="teacherId"/>
</parameterMap>
<sql id="courceSql">id,cource_name,teacher_id</sql>
<select id="selectAll" resultMap="courceMap">
select <include refid="courceSql"/> from cource
</select>
<select id="selectById" parameterType="int" resultMap="courceMap">
select <include refid="courceSql"/> from cource where
id=#{id,javaType=int,jdbcType=INTEGER}
</select>
<insert id="addCource" parameterMap="paraMap">
insert into cource (<include refid="courceSql"/>) values(
#{id,javaType=int,jdbcType=INTEGER},
#{courceName,javaType=String,jdbcType=VARCHAR},
#{teacherId,javaType=int,jdbcType=INTEGER}
)
</insert>
<update id="updateCourceById" parameterType="com.mall.dubbo.entity.Cource">
update cource set
cource_name=#{courceName,javaType=String,jdbcType=VARCHAR},
teacher_id=#{teacherId,javaType=int,jdbcType=INTEGER}
where id=#{id,javaType=int,jdbcType=INTEGER}
</update>
<delete id="deleteCourceById" parameterType="int">
delete from cource where id=#{id,javaType=int,jdbcType=INTEGER}
</delete>
<delete id="deleteAll">
delete from cource
</delete>
</mapper>
student.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.mall.dubbo.dao.StudentDao">
<resultMap type="com.mall.dubbo.entity.Student" id="studentMap">
<id property="id" column="id" javaType="int" jdbcType="INTEGER"/>
<result property="age" column="age" javaType="int" jdbcType="INTEGER"/>
<result property="studentName" column="student_name" javaType="String" jdbcType="VARCHAR"/>
<result property="sex" column="sex" javaType="int" jdbcType="INTEGER"/>
<result property="teacherId" column="teacher_id" javaType="int" jdbcType="INTEGER"/>
</resultMap>
<parameterMap type="com.mall.dubbo.entity.Student" id="parameMap">
<parameter property="id"/>
<parameter property="age"/>
<parameter property="studentName"/>
<parameter property="sex"/>
<parameter property="teacherId"/>
</parameterMap>
<sql id="studentSql">id,age,student_name,sex,teacher_id</sql>
<select id="selectAll" resultMap="studentMap">
select <include refid="studentSql"/> from student
</select>
<select id="selectById" parameterType="int" resultMap="studentMap">
select <include refid="studentSql"/> from student where id=#{id,javaType=int,jdbcType=INTEGER}
</select>
<insert id="addStudent" parameterType="com.mall.dubbo.entity.Student">
insert into student(<include refid="studentSql"/>) values (
#{id,javaType=int,jdbcType=INTEGER},
#{age,javaType=int,jdbcType=INTEGER},
#{studentName,javaType=String,jdbcType=VARCHAR},
#{sex,javaType=int,jdbcType=INTEGER},
#{teacherId,javaType=int,jdbcType=INTEGER}
)
</insert>
<update id="updateStudentById" parameterMap="parameMap">
update student set
age=#{age,javaType=int,jdbcType=INTEGER},
student_name=#{studentName,javaType=String,jdbcType=VARCHAR},
sex=#{sex,javaType=int,jdbcType=INTEGER},
teacher_id=#{teacherId,javaType=int,jdbcType=INTEGER}
where id=#{id,javaType=int,jdbcType=INTEGER}
</update>
<delete id="deleteStudentById" parameterType="int">
delete from student where id=#{id,javaType=int,jdbcType=INTEGER}
</delete>
<delete id="deleteAll">
delete from student
</delete>
</mapper>
teacher.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.mall.dubbo.dao.TeacherDao">
<resultMap type="com.mall.dubbo.entity.Teacher" id="TeacherMap">
<id property="id" column="id" javaType="int" jdbcType="INTEGER" />
<result property="teacherName" column="teacher_name" javaType="String"
jdbcType="VARCHAR" />
<result property="age" column="age" javaType="int" jdbcType="INTEGER" />
<result property="sex" column="sex" javaType="int" jdbcType="INTEGER" />
<!-- 方法一 -->
<!-- 使用这种方式,coulumn的名字不能重复,否则识别不了 -->
<association property="cource" javaType="com.mall.dubbo.entity.Cource">
<id property="id" column="cource_id" javaType="int" jdbcType="INTEGER" />
<result property="courceName" column="cource_name" javaType="String"
jdbcType="VARCHAR" />
<result property="teacherId" column="c_teacher_id" javaType="int"
jdbcType="INTEGER" />
</association>
<!-- 方法二 -->
<!-- <association property="cource" javaType="com.mall.dubbo.entity.Cource"
resultMap="courceMap"/> -->
<!-- 方法三 -->
<association property="cource" javaType="com.mall.dubbo.entity.Cource"
column="teacher_id" select="selectCourceById"/>
<!-- 方法一 -->
<!-- 使用这种方式,coulumn的名字不能重复,否则识别不了 -->
<collection property="students" ofType="com.mall.dubbo.entity.Student">
<id property="id" column="student_id" javaType="int" jdbcType="INTEGER" />
<result property="age" column="s_age" javaType="int" jdbcType="INTEGER" />
<result property="studentName" column="student_name" javaType="String"
jdbcType="VARCHAR" />
<result property="sex" column="s_sex" javaType="int" jdbcType="INTEGER" />
<result property="teacherId" column="s_teacher_id" javaType="int"
jdbcType="INTEGER" />
</collection>
<!-- 方法二 -->
<!-- <collection property="students" ofType="com.mall.dubbo.entity.Student"
resultMap="studentMap"/> -->
<!-- 方法三 -->
<!-- <collection property="students" ofType="com.mall.dubbo.entity.Student"
column="teacher_id" select="selectStudentByTeacherId"/> -->
</resultMap>
<!-- 对应于方法二 -->
<!-- <resultMap type="com.mall.dubbo.entity.Cource" id="courceMap"> <id
property="id" column="cource_id" javaType="int" jdbcType="INTEGER"/>
<result property="courceName" column="cource_name" javaType="String" jdbcType="VARCHAR"/>
<result property="teacherId" column="c_teacher_id" javaType="int" jdbcType="INTEGER"/>
</resultMap> -->
<!-- 对应于方法二 -->
<!-- <resultMap type="com.mall.dubbo.entity.Student" id="studentMap"> <id
property="id" column="student_id" javaType="int" jdbcType="INTEGER"/>
<result property="age" column="s_age" javaType="int" jdbcType="INTEGER"/>
<result property="studentName" column="student_name" javaType="String" jdbcType="VARCHAR"/>
<result property="sex" column="s_sex" javaType="int" jdbcType="INTEGER"/>
<result property="teacherId" column="s_teacher_id" javaType="int" jdbcType="INTEGER"/>
</resultMap> -->
<parameterMap type="com.mall.dubbo.entity.Teacher" id="paraMap">
<parameter property="id" />
<parameter property="teacherName" />
<parameter property="age" />
<parameter property="sex" />
</parameterMap>
<sql id="teacherSql">id,teacher_name,age,sex</sql>
<!-- 对应于方法三 -->
<!-- <select id="selectCourceByTeacherId" parameterType="int" resultType="com.mall.dubbo.entity.Cource">
select * from cource where teacher_id=#{id,javaType=int,jdbcType=INTEGER}
</select>
<select id="selectStudentByTeacherId" parameterType="int" resultType="com.mall.dubbo.entity.Student">
select * from student where teacher_id=#{id,javaType=int,jdbcType=INTEGER}
</select> -->
<select id="selectTeacerById" parameterType="int" resultMap="TeacherMap">
select t.*,s.id student_id,s.age s_age,s.sex s_sex,s.teacher_id s_teacher_id,s.student_name,
c.id cource_id,c.cource_name,c.teacher_id c_teacher_id
from teacher t join cource c on t.id=c.teacher_id left outer join student
s on t.id=s.teacher_id
where t.id=#{id,javaType=int,jdbcType=INTEGER}
</select>
<!-- <select id="selectTeacerById" parameterType="int" resultMap="TeacherMap">
select * from teacher where id=#{id,javaType=int,jdbcType=INTEGER}
</select> -->
<select id="selectAll" resultMap="TeacherMap">
select t.*,s.id student_id,s.age s_age,s.sex s_sex,s.teacher_id s_teacher_id,s.student_name,
c.id cource_id,c.cource_name,c.teacher_id c_teacher_id
from teacher t join cource c on t.id=c.teacher_id left outer join student
s on t.id=s.teacher_id
</select>
<insert id="addTeacher" parameterMap="paraMap">
insert into teacher(
<include refid="teacherSql" />
)values(
#{id,javaType=int,jdbcType=INTEGER},
#{teacherName,javaType=String,jdbcType=VARCHAR},
#{age,javaType=int,jdbcType=INTEGER},
#{sex,javaType=int,jdbcType=INTEGER}
)
</insert>
</mapper>
以上就是三者的映射关系,在这里对于student.xml和cource.xml我不想过多的去说,大家注意resultMap,resultType,paramType,paramMap这四个属***的不同就行
我重点讲一下teacher.xml文件中的映射关系,这里面涉及到collection和association这两个元素,这两个元素分别代表者1对多和1对1的关系,需要注意的是,
查询的结果总不要有相同的字段名,如果存在相同的字段名会覆盖,从而导致查询的结果不对。
5、对应的Dao接口
GenericDao:
package com.mall.dubbo.dao;
public interface GenericDao {
}
该接口是所有的dao接口的父接口,对于与配置文件中的markerInterface
CourceDao:
package com.mall.dubbo.dao;
import org.springframework.stereotype.Repository;
import com.mall.dubbo.entity.Cource;
@Repository
public interface CourceDao extends GenericDao {
public abstract void addCource(Cource cource);
public abstract Cource selectById(int id);
public abstract void updateCourceById(Cource cource);
}
StudentDao:
package com.mall.dubbo.dao;
import com.mall.dubbo.entity.Student;
@Repository
public interface StudentDao extends GenericDao {
public abstract void addStudent(Student student);
}
TeacherDao:
package com.mall.dubbo.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.mall.dubbo.entity.Teacher;
@Repository
public interface TeacherDao extends GenericDao{
public abstract void addTeacher(Teacher teacher);
public abstract Teacher selectTeacerById(int id);
public abstract List<Teacher> selectAll();
}
6、编写测试类
CourceTest.java:
package com.mall.dubbo.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.mall.dubbo.dao.CourceDao;
import com.mall.dubbo.entity.Cource;
public class CourceTest {
private ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-mybatis.xml");
@Test
public void addCource(){
Cource cource = new Cource();
cource.setCourceName("English");
cource.setId(2);
cource.setTeacherId(1);
CourceDao dao = context.getBean(CourceDao.class);
dao.addCource(cource);
}
//@Test
public void getCourceById(){
CourceDao dao = context.getBean(CourceDao.class);
Cource c = dao.selectById(1);
System.out.println(c);
}
//@Test
public void updateCourceById(){
Cource cource = new Cource();
cource.setCourceName("France");
cource.setId(1);
cource.setTeacherId(2);
CourceDao dao = context.getBean(CourceDao.class);
dao.updateCourceById(cource);
}
}
StudentTest.java:
package com.mall.dubbo.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.mall.dubbo.dao.StudentDao;
import com.mall.dubbo.entity.Student;
public class StudentTest {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-mybatis.xml");
StudentDao dao = context.getBean(StudentDao.class);
@Test
public void addStudentTest(){
Student stu = new Student();
stu.setAge(21);
stu.setId(3);
stu.setSex(1);
stu.setStudentName("zhangqi");
stu.setTeacherId(1);
dao.addStudent(stu);
}
}
TeacherTest.java:
package com.mall.dubbo.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.mall.dubbo.dao.TeacherDao;
import com.mall.dubbo.entity.Cource;
import com.mall.dubbo.entity.Student;
import com.mall.dubbo.entity.Teacher;
public class TeacherTest {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-mybatis.xml");
TeacherDao dao = context.getBean(TeacherDao.class);
//@Test
public void addTeacherTest(){
Teacher t = new Teacher();
t.setAge(45);
t.setId(1);
t.setSex(0);
t.setTeacherName("liusanming");
dao.addTeacher(t);
}
@Test
public void getTeacherById(){
Teacher t = dao.selectTeacerById(1);
Cource c = t.getCource();
List<Student> stus = t.getStudents();
System.out.println(stus+","+stus.size());
System.out.println(c);
System.out.println(t);
}
//@Test
public void getTeachers(){
List<Teacher> ts = dao.selectAll();
System.out.println(ts.size());
Teacher t = ts.get(0);
Cource c = t.getCource();
List<Student> stus = t.getStudents();
System.out.println(stus+","+stus.size());
System.out.println(c);
System.out.println(t);
}
}
到此整个的开发过程就结束了,内容有点多,希望能够对大家有帮助。大家如果有什么问题,请留言!
本人也毕业未满一年,如果讲的有什么不对的地方,请指正,我们一块进步,谢谢!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341