一、包依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId> <!-- mybatis -->
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId> <!-- mybatis -->
<version>1.2.0</version>
</dependency>
二、配置文件(applicationContext-mybatis.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource_dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="password" value="root"/>
<property name="username" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/ppt?useUnicode=true&characterEncoding=UTF-8"/>
<property name="maxActive" value="20"/>
<property name="maxIdle" value="15"/>
<property name="minIdle" value="5"/>
<property name="maxWait" value="60000"/>
<property name="initialSize" value="1"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="180"/>
<property name="logAbandoned" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
<property name="minEvictableIdleTimeMillis" value="3600000"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource_dbcp"/>
</property>
<property name="mapperLocations" value="mybatis/*"></property>
</bean>
<!--
<bean id="mybatisDemoInterface" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.csair.dao.MybatisDemoInterface"/>
<property name="sqlSessionFactory"> <!--自动代理,帮助实现接口-->
<ref local="sqlSessionFactory"/>
</property>
</bean>
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.csair.dao"></property> <!-- 扫描接口定义的方式,自动帮实现 -->
</bean>
<bean id="mybatisDemo" class="com.csair.dao.MybatisDemo">
<property name="mybatisDemoInterface"><ref local="mybatisDemoInterface"/></property>
</bean>
</beans>
三、查询语句映射文件(UserAnotationMapper.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="com.csair.dao.MybatisDemoInterface">
<select id="selectUser" resultType="com.csair.dao.UserAnotation">
select * from user
</select>
<insert id="addUser">
insert into user(username, password) value('mybatis', 'mybatis')
</insert>
</mapper>
四、接口
public interface MybatisDemoInterface {
List<UserAnotation> selectUser();
void addUser();
}
五、测试类
public class MybatisDemo {
private MybatisDemoInterface mybatisDemoInterface;
public static void main(String[] args) {
String configLocation = "applicationContext-mybatis.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
MybatisDemo demo = context.getBean("mybatisDemo", MybatisDemo.class);
demo.query();
}
public void query() {
List<UserAnotation> user = mybatisDemoInterface.selectUser();
System.out.println(user.toString());
}
public void insert() {
mybatisDemoInterface.addUser();
}
public MybatisDemoInterface getMybatisDemoInterface() {
return mybatisDemoInterface;
}
public void setMybatisDemoInterface(MybatisDemoInterface mybatisDemoInterface) {
this.mybatisDemoInterface = mybatisDemoInterface;
}
}