1.填写spring-mybatis.xml文件,(也就是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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-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">
<!-- 自动扫描 -->
<context:component-scan base-package="com.das"/>
<context:property-placeholder location="classpath:a.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
<property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
<property name="testOnReturn" value="${jdbc.testOnReturn}" />
<property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
<property name="logAbandoned" value="${jdbc.logAbandoned}" />
<property name="filters" value="${jdbc.filters}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 引入mybatis的相关配置文件 -->
<property name="configLocation" value="mybatis-config.xml"></property>
</bean>
<!-- 创建接口实现类的实例 -->
<bean id="TeacherDao" class="com.das.dao.impl.TeacherImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
2.填写mybatis-config.xml文件(用resource加载映射文件方式)
<?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>
<mappers>
<!-- xml文件全路径,注意路径书写方式 -->
<mapper resource="com/das/pojo/TeacherDao.xml"/>
</mappers>
</configuration>
3.创建javaBean
package com.das.pojo;
public class Teacher {
Integer id;
String name;
Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
4.创建接口TeacherDao
package com.das.dao;
import com.das.pojo.Teacher;
public interface TeacherDao {
int insertTeacher(Teacher teacher);
}
5.创建接口实现类,继承SqlSessionDaoSupport类,此类中有sqlSession的get和set方法.
package com.das.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.das.dao.TeacherDao;
import com.das.pojo.Teacher;
public class TeacherImpl extends SqlSessionDaoSupport implements TeacherDao {
@Override
public int insertTeacher(Teacher teacher) {
SqlSession sqlSession = this.getSqlSession();
return sqlSession.insert("teacher.insertTeacher",teacher);
}
}
6.创建xml映射文件,其中namespace可随意填写,可置于工程任何位置.
<?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="teacher">
<insert id="insertTeacher">
insert into teacher(name,age) values(#{name},#{age})
</insert>
</mapper>