1.1添加maven jarjava
pom文件添加 mybatis-spring mybatis ,使用阿里的druid控制,mysql <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency>
1.2按本身的要求看本身是否是須要吧spring-mabtis的配置文件抽出來,若是不須要的話,在spring-config.xml添加就能夠mysql
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據庫基本信息配置 --> <property name="url" value="${druid.url}" /> <property name="username" value="${druid.username}" /> <property name="password" value="${druid.password}" /> <property name = "driverClassName" value = "${druid.driverClassName}" /> <!-- 初始化鏈接數量 --> <property name="initialSize" value="${druid.initialSize}" /> <!-- 最小空閒鏈接數 --> <property name="minIdle" value="${druid.minIdle}" /> <!-- 最大併發鏈接數 --> <property name="maxActive" value="${druid.maxActive}" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="${druid.maxWait}" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${druid.validationQuery}" /> <property name="testWhileIdle" value="${druid.testWhileIdle}" /> <property name="testOnBorrow" value="${druid.testOnBorrow}" /> <property name="testOnReturn" value="${druid.testOnReturn}" /> <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小 若是用Oracle,則把poolPreparedStatements配置爲true,mysql能夠配置爲false。 --> <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" /> <!-- 配置監控統計攔截的filters --> <property name="filters" value="${druid.filters}" /> </bean> <!--建立sessionFactoryBean --> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/ecloud/mapper/*.xml"/> <!--<property name="configLocation" value="classpath:mybatis-config.xml"/>--> </bean> <!--檢測,該包下的接口自動注入--> <mybatis:scan base-package="com.ecloud.dao"/> <!--事物--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
1.3編寫mapper.xml 或者用註解的方式spring
<?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.ecloud.dao.UserDao" > <select id="getUsers" resultType="com.ecloud.entity.User" > SELECT * FROM users; </select> </mapper>
import java.util.List; public interface UserDao { @Select("SELECT * FROM users;") List<User> getUsers(); }