Spring+SpringMVC+mybatis+easyui整合-----注解方式的mybatis的使用

来源:互联网 发布:新海典软件怎么用 编辑:程序博客网 时间:2024/06/06 18:02

  • 项目结构说明
  • spring整合mybatis及mybatis使用测试(注解、非注解)

一、项目结构:

这里写图片描述 
其中applicationContext.xml为spring配置文件,我们先把他放在src下,用于测试mybatis

二、mybatis使用

我们使用一个简单的例子来说明一下spring中如何整合非注解形式和注解形式的mybatis,以及mybatis的测试。 
首先导入spring核心jar、mybatis jar、spring整合mybatis jar 
编写实体类:


@Component public class Student { private int id; private String name; private String password; public int getId() { return</span> id; } public void setId(int id) { this.id = id; } public String getName() {return name;} public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }} 

dao层的mybatis可以使用注解方式也可以使用非注解的方式,我们一一来看一下。

对于非注解形式: 
在com.etoak.dao 中编写类StudentDaoIF.java

 @Repository public interface StudentDaoIF { public int addStudent(Student stu); public int delStudentById(int id); public int updateStudent(Student stu); public Student selectStudentById(int id); public List<Student> selectAllStudents(); public int StudentCount();public List<Student> selectStudentByPage(Map map); } 

同时在这个包下添加xml配置: 
StudentDaoIF-mapper.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.etoak.dao.StudentDaoIF" > <insert id="addStudent" parameterType= "com.etoak.entity.Student">insert into student values(null,#{name},#{password});  </insert><delete id="delStudentById" parameterType="java.lang.Integer"> delete from student where id=#{id};  </delete><update id="updateStudent" parameterType="com.etoak.entity.Student">update student set name=#{name},password=#{password} where id=#{id}; </update><!-- resultMap: 返回数据类型 需要在下文中解释 --><select id="selectStudentById" parameterType="java.lang.Integer" resultMap=:"student"> select * from student where id=#{id}; </select> <!-- 这里返回的是list,但list里面存放的还是city对象,所以还是city --><select id="selectAllStudents" resultMap="student">select * from student; </select><select id="selectStudentCount" resultType="java.lang.Integer">select count(*) from student;</select><select id="selectStudentByPage" parameterType="java.util.Map" resultMap=le="student"> select * from student limit #{start},#{max}; </select><!-- 返回类型解释 --><resultMap type="com.etoak.entity.Student" id="student"><result property="id" column="id"/><result property="name" column="name"/><result property="password" column="password"/></resultMap></mapper>

applicationContext中添加:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com"></context:component-scan><mvc:annotation-driven /> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"></property><property name="mapperLocations"> <list> <value>classpath:com/etoak/dao/StudentDaoIF-mapper.xml</value> </list> </property> </bean> <bean  id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/etoak"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.etoak.dao" /> <property name="sqlSessionFactoryBeanName" value="ssf"></property></bean></beans>

编写测试类:

public class TestMybatis { public static void(String[] args) { ApplicationContext ac =  new ClassPathXmlApplicationContext("applicationContext.xml"); ac = new FileSystemXmlApplicationContext("src/applicationContext.xml"); StudentDaoIF dao = (StudentDaoIF)ac.getBean("studentDaoIF");Student stu = (Student)ac.getBean("student"); stu.setName("a"); stu.setPassword("a1");dao.addStudent(stu); } } 

对于注解方式的mybatis: 
需要去掉StudentDaoIF-mapper.xml配置,在StudentDaoIF接口中添加注解,同时applicationContext.xml中注释掉如下几行:

<property name="mapperLocations"><list><value>classpath:com/etoak/dao/StudentDaoIF-mapper.xml</value></list></property>

添加注解的dao层接口:

@Repositorypublic interface StudentDaoIF {@Insert("insert into student values(null,#{name},#{password})") @Options(useGeneratedKeys = true , keyProperty = "id") public int addStudent(Student stu); @Delete("delete from student where id=#{id}") public int delStudentById(int id); @Update("update student set name=#{name},password=#{password} where id=#{id}")public int updateStudent(Student stu); @Select("select * from student where id=#{id}")  @Results({ @Result(column = "id" , property ="id"), @Result(column ="name", property ="name"),@Result(column ="password",property ="password")}) public Student selectStudentById(int id); @Select("select * from student") @Results({ @Result(column ="id", property ="id"),@Result(column ="name", property ="name"),@Result(column ="password", property ="password")}) public List<Student> selectAllStudents();@Select("select count(*) from student") public int StudentCount(); @Select(" select * from student limit #{start},#{max};") @Results({ @Result(column ="id", property ="id"),@Result(column ="name", property ="name"),@Result(column ="password", property ="password") }) public List<Student> selectStudentByPage(Map map); }

我们同样可以使用之前的测试类进行测试。

                                             
0 0