1.添加jar包,Spring与mybatis的整合jar包,spring的核心jar包,mybatis的jar包等。
2.配置sql映射文件,内容包括根节点mapper,其属性namespace等于sql文件映射的接口路径,还包括insert,update等要实现的数据库操作。
<?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.abc.dao.IStudentDao" >
< insert id = " insertStudent" >
insert into student (name,age,score) values(#{name},#{age},#{score})
</ insert>
< delete id = " deleteStudentById" >
delete from student where id=#{xxx}
</ delete>
< update id = " updateStudent" >
update student set name=#{name},age=#{age},score=#{score} where id=#{id}
</ update>
< select id = " selectStudentById" resultType = " Student" >
select id,name,age,score from student where id=#{ooo}
</ select>
< select id = " selectAllStudent" resultType = " Student" >
select id,name,age,score from student
</ select>
</ mapper>
3.配置mybatis的配置文件,在此配置中不用配置数据源,需要定义别名方便后面对某个包中的属性名的应用,就不用写完整的路径名,定义sql的映射文件,此文件名应与持久化类(持久化类是指其实例状态需要被myBaties持久化到数据库中的类)名一致,并放在同一个包下。
<?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>
< package name = " com.abc.beans" />
</ typeAliases>
< mappers>
< package name = " com.abc.dao" />
</ mappers>
</ configuration>
4.配合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"
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" >
通过DruidDataSource配置数据源
< bean id = " myDataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< 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>
定义一个外部数据库信息配置,方便使用。
< context: property-placeholder location = " jdbc.properties" />
因为项目中并没有MapperFactoryBean类,所以需要SqlSessionFactoryBean类来生成,还需要配置其dataSource来配置数据源信息和configLocation来设置myBatis主配置文件。
< bean id = " mySqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " myDataSource" />
< property name = " configLocation" value = " mybatis.xml" />
</ bean>
生成dao需要mybatis主配置文件,已有主配置文件说明映射文件需要数据源,在mybatis与spring的整合中数据源要写在spring的配置文件中,所以还需要在此配置数据源信息。
定义dao对象,但是没有dao的实现类,所以通过MapperFactoryBean生成,还需要指定映射的哪个接口
< bean id = " studentDao" class = " org.mybatis.spring.mapper.MapperFactoryBean" >
< property name = " sqlSessionFactory" ref = " mySqlSessionFactory" />
< property name = " mapperInterface" value = " com.abc.dao.IStudentDao" />
</ bean>
< bean id = " studentService" class = " com.abc.service.StudentServiceImpl" >
< propert name = " dao" ref = " studentDao" />
</ bean>
</ beans>
5.完成。