Spring整合Mybatis(采用Mybatis配置文件方式)
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。(来自百度百科)
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs)(来自百度百科)
以下皆为原创内容
环境
maven3.6.3,tomcat8,jdk1.8,STS,java ee
所用jar包
Spring配置文件:applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 1) 读取properties中的内容 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2) 直接数据库,采用阿里巴巴的druid数据库池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置mybatis全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="mybatis-config.xml"/>
</bean>
<!-- 4:配置扫描Dao接口包,动态实现DAO接口,注入到spring容器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描的Dao接口 -->
<property name="basePackage" value="dao"/>
</bean>
</beans>
Mybatis配置文件:mybatis-config.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 类型别名 -->
<typeAliases>
<typeAlias type="pojo.Student" alias="Student"/>
<typeAlias alias="Course" type="pojo.Course" />
<typeAlias alias="SC" type="pojo.SC" />
</typeAliases>
<!-- 映射文件 -->
<mappers>
<mapper resource="dao/StudentMapper.xml" />
<mapper resource="dao/CourseMapper.xml" />
<mapper resource="dao/SCMapper.xml" />
</mappers>
</configuration>
数据库连接配置文件:jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/Student?useUnicode=true&characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
实体类:Course.java
package pojo;
public class Course {
private String Con;
private String Cname;
private String Cpno;
private String Ccredit;
public Course() {
super();
// TODO Auto-generated constructor stub
}
public String getCon() {
return Con;
}
public void setCon(String con) {
Con = con;
}
public String getCname() {
return Cname;
}
public void setCname(String cname) {
Cname = cname;
}
public String getCpno() {
return Cpno;
}
public void setCpno(String cpno) {
Cpno = cpno;
}
public String getCcredit() {
return Ccredit;
}
public void setCcredit(String ccredit) {
Ccredit = ccredit;
}
@Override
public String toString() {
return "Course [Con=" + Con + ", Cname=" + Cname + ", Cpno=" + Cpno + ", Ccredit=" + Ccredit + "]";
}
}
数据库表
映射文件:CourseMapper.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="dao.CourseMapper">
<resultMap type="Course" id="CourseResult">
<id property="Con" column="Con" />
<result property="Cname" column="Cname" />
<result property="Cpno" column="Cpno" />
<result property="Ccredit" column="Ccredit" />
</resultMap>
<select id="findAllCourse" resultMap="CourseResult">
select * from course
</select>
<select id="findCourseByCon" parameterType="String" resultType="Course">
select * from course where Con=#{Con}
</select>
<insert id="insertCourse" parameterType="Course">
insert into course(Con,Cname,Cpno,Ccredit) values (#{Con},#{Cname},#{Cpno},#{Ccredit})
</insert>
</delete>
</mapper>
映射接口:CourseMapper.java
package dao;
import java.util.List;
import pojo.Course;
public interface CourseMapper {
List<Course> findAllCourse();
Course findCourseByCon(String Con);
}
测试类:springAndMybatisTest.java
package Test;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.CourseMapper;
import dao.SCMapper;
import dao.StudentMapper;
import pojo.Course;
import pojo.SC;
import pojo.Student;
public class springAndMybatisTest {
//获得spring容器
static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
public void student() {
// 利用反射,得到spring自动配置的 ProductMapper 的 <bean>
StudentMapper studentMapper = context.getBean(StudentMapper.class);
List<Student> findAllStudents = studentMapper.findAllStudents();
findAllStudents.forEach(System.out::println);
}
public void CourseMapper() {
System.out.println("1");
SCMapper SCMapper = context.getBean(SCMapper.class);
List<SC> findAllSC = SCMapper.findAllSC();
findAllSC.forEach(System.out::println);
System.out.println("2");
}
public static void main(String[] args) {
CourseMapper courseMapper = context.getBean(CourseMapper.class);
List<Course> findAllCourse = courseMapper.findAllCourse();
findAllCourse.forEach(System.out::println);
}
}