Spring整合myBatis三种方式
1.有dao层接口的实现类
存在问题:要自己编写实现类
2.去掉实现类,在配置文件中配置关联接口
存在问题:要在配置文件中一个一个的关联接口,造成applicationContext.xml过于复杂
3.开启接口的自动扫描
这三种方式都是在原来的基础之上,不断地优化,已达到让代码变得更加简洁,这里我用myeclipse依次演示一波
一、第一种实现:
1.导入jar包,把ssm的所有jar包往里直接放,外加junit,log4j,commons-logging的jar包,因为我也是个小白,没怎么研究jar包的问题,就全弄进去了。
2.根据我自己创建的数据库,新建一个com.wangningbo.bean.Student实体类
package com.wangningbo.bean;
public class Student {
private Integer id;
private String name;
private int age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
3.创建com.wangningbo.dao.IStudentDao接口,这里为了方便,就定义了插入和查询方法
package com.wangningbo.dao;
import java.util.List;
import com.wangningbo.bean.Student;
public interface IStudentDao {
List<Student> selectAllStudent();
void insertStudent(Student student);
}
4.定义mapper.mapper.xml来对定义sql语句
<?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.wangningbo.dao.IStudentDao">
<!-- SQL语句 -->
<insert id="insertStudent" parameterType="com.wangningbo.bean.Student">
insert into student(name,age) values(#{name},#{age})
</insert>
<select id="selectAllStudent" resultType="com.wangningbo.bean.Student">
select * from student
</select>
</mapper>
5.创建StudentDaoImpl实现类,来对IStudentdao接口进行实现
package com.wangningbo.dao;
import java.util.List;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.wangningbo.bean.Student;
public class StudentDaoImpl extends SqlSessionDaoSupport implements IStudentDao {
@Override
public void insertStudent(Student student) {
this.getSqlSession().insert("insertStudent",student);
//这里不需要提交事务
}
@Override
public List<Student> selectAllStudent() {
List<Student> selectList = this.getSqlSession().selectList("selectAllStudent");
return selectList;
}
}
6.最后我们来编写Spring核心文件,这约束好像不全,但是在这里够用了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 使用properties配置文件 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 创建SqlSessionFactory -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 加载Sql配置文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 注冊service,将SqlSessionFactory注入 -->
<bean id="studentService" class="com.wangningbo.dao.StudentDaoImpl">
<property name="sqlSessionFactory" ref="mySqlSessionFactory"/>
</bean>
</beans>
7.最后编写com.wangningbo.test.test测试方法
package com.wangningbo.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wangningbo.bean.Student;
import com.wangningbo.dao.IStudentDao;
public class test {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStudentDao service =(IStudentDao) context.getBean("studentService");
Student student = new Student("WNB",20);
service.insertStudent(student);
List<Student> students = service.selectAllStudent();
for (Student student1: students) {
System.out.println(student1);
}
}
}
二、第二种实现:
1.照着第一种方式的1,2,3,4依次创建
2.这里不用创建IStudentDaoImpl,我们直接对Spring配置文件进行配置,把接口进行关联
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 使用properties配置文件 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 创建SqlSessionFactory -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 加载Sql配置文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 没了实现类,现在要配置mapper接口 -->
<bean id="myMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 关联mapper接口 -->
<property name="mapperInterface" value="com.wangningbo.dao.IStudentDao"/>
<!-- 关联SqlSessionFactory -->
<property name="sqlSessionFactory" ref="mySqlSessionFactory"/>
</bean>
</beans>
3.最后编写com.wangningbo.test.test测试方法
package com.wangningbo.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wangningbo.bean.Student;
import com.wangningbo.dao.IStudentDao;
public class test {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStudentDao service =(IStudentDao) context.getBean("myMapper");
Student student = new Student("WNB",20);
service.insertStudent(student);
List<Student> students = service.selectAllStudent();
for (Student student1: students) {
System.out.println(student1);
}
}
}
三、第三种实现:
1.基本和第二种相同,唯一需要改动的就是spring配置文件,约束文件够用就行,我这里新加了约束,可以和上面一样,不要在意
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 注册properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 使用properties配置文件 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 创建SqlSessionFactory -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 加载Sql配置文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 直接配置mapper接口自动扫描,不一个个配置了 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wangningbo.dao"/>
</bean>
</beans>
2.编写com.wangningbo.test.test测试方法
package com.wangningbo.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wangningbo.bean.Student;
import com.wangningbo.dao.IStudentDao;
public class test {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStudentDao service =(IStudentDao) context.getBean("IStudentDao");
Student student = new Student("WNB",20);
service.insertStudent(student);
List<Student> students = service.selectAllStudent();
for (Student student1: students) {
System.out.println(student1);
}
}
}