导入相应的jar包
先写好db.properties文件,用于存放数据库的相关信息
driver=com.mysql.jdbc.Driver
password=root
username=root
url=jdbc\:mysql\://localhost\:3306/he
新建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/db.properties"/>
<property name="fileEncoding" value="utf-8" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<context:annotation-config/>
<context:component-scan base-package="com.mybatis"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/mybatis/mapper/*"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mybatis.dao"/>
</bean>
</beans>
在web.xml中填写相应的信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mybatis.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<display-name>时间的监听器</display-name>
<listener-class>com.mybatis.utils.Timers</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在src中新建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.mybatis.dao.UserMapper">
<select id="getUser" resultMap="userMap">
select*from xiu
</select>
<select id="login" parameterType="com.mybatis.entity.User" resultType="java.lang.Integer">
select count(*) from xiu where username=#{username} and password=#{password}
</select>
</mapper>
新建的resultmap
<?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.mybatis.dao.UserMapper">
<resultMap type="com.mybatis.entity.User" id="userMap">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
</mapper>
利用mybatis的接口编程
package com.mybatis.dao;
import java.util.List;
import com.mybatis.entity.User;
/**
*
* @author 戴礼明
*日期: 2015-12-9
*content:用户接口
*/
public interface UserMapper {
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> *用户登录
<span style="white-space:pre"> </span> * @param user 需要登录的用户
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public int login(User user);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 查询所有的用户
<span style="white-space:pre"> </span> * @return 返回查询结果
<span style="white-space:pre"> </span> */
public List<User> getUser();<span style="white-space:pre"> </span>
}