前言:本文是在SSM三大框架环境搭建之SpringMVC基础上写的,如果有什么疑问请点击前面的超链接参考之前的帖子。
1.pom.xml添加依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.29</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
</dependencies>
2.写配置数据源所需要的(jdbcDriver、url、username、password)
为了便于后期修改所以这里我们写成一个properties文件
jdbcdriver:com.mysql.jdbc.Driver
jdbcurl:jdbc:mysql:///jtsys
jdbcusername:root
jdbcpassword:1806
3.配置druid数据源
<!-- 引入我们所写的文件 -->
<util:properties id="mysql" location="classpath:mysql.properties"/>
<!-- 整合Druid连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"
destroy-method="close"
lazy-init="false">
<property name="Url" value="#{mysql.jdbcurl}"></property>
<property name="DriverClassName" value="#{mysql.jdbcdriver}"></property>
<property name="Username" value="#{mysql.jdbcusername}"></property>
<property name="Password" value="#{mysql.jdbcpassword}"></property>
</bean>
4.配置SqlSessionFactoryBean
MapperLocations的具体写法是参照源代码的注释写的,相信大家一看就懂
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="DataSource" ref="DataSource"></property>
<property name="MapperLocations" value="classpath*:Mapper/*Mapper.xml" ></property>
</bean>
5.配置我们的Dao层扫描文件
<!-- 配置dao接口扫描,底层会基于dao接口创建这个接口的代理对象,这个代理
对象内部会通过mybatis访问数据库 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="BasePackage" value="com.jt.dao"></property>
<property name="SqlSessionFactoryBeanName" value="sqlsessionfactory"></property>
</bean>
6.写dao层
根据id从用户表中查询用户名
package com.jt.dao;
public interface testDao {
String findUserByid(int id);
}
7.写mapper文件
<?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.jt.dao.testDao">
<select id="findUserByid" resultType="String">
select username
from sys_users
where id=#{id}
</select>
</mapper>
写到这里mybatis的基本功能已经写完了,大家可以写junit测试类测试,这里我就多加赘述,下一篇我会写到用Spring管理的Service层结合SpringMVC进行测试.