1.引入相应的jar包。(Mybatis的jar包,Spring的jar包,mybatis-spring-1.1.1.jar,以及c3p0的包)。
2.编写相应的包(三层的包)。搭建。
3.配置相应的spring的配置。
1)配置相应的数据源的配置。
<?xml version="1.0" encoding="UTF-8"?><beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="jdbcDataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<property name="user">
<value>bbss</value>
</property>
<property name="password">
<value>123</value>
</property>
<property name="initialPoolSize">
<value>10</value>
</property>
</bean>
2)配置mybatis的SqlSessionFactory(也需要在ApplicationContext里做配置)。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
4.搭建mybatis的框架了(编写相应的实体类,SQL映射文件)。比如:
<?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.dao.imp.UserMapper">
<select id="getModel" resultType="com.model.UserInfo">
select * from UserInfo where userId = #{userId}
</select>
<select id="getUsers" parameterType="java.lang.String" resultType="com.model.UserInfo" >
select * from UserInfo where userName like '%${value}%'
</select>
<delete id="removeItem">
delete from UserInfo where userId=#{userId}
</delete>
<insert id="addItem" parameterType="com.model.UserInfo">
insert into UserInfo(userid,username,userpwd,usermail,address,reason)
values(usernext.nextval,#{userName},#{userPwd},#{userMail},#{address},#{reason})
</insert>
<insert id="getInsertUserId" parameterType="com.model.UserInfo">
<selectKey keyProperty="userId" resultType="int" order="BEFORE">
select usernext.nextval from dual
</selectKey>
insert into UserInfo(userid,username,userpwd,usermail,address,reason)
values(#{userId},#{userName},#{userPwd},#{userMail},#{address},#{reason})
</insert>
<update id="updateItem" parameterType="com.model.UserInfo">
update UserInfo set
<if test= "userName!=null">
userName=#{userName},
</if>
<if test= "userPwd!=null">
userPwd=#{userPwd},
</if>
<if test= "userMail!=null">
userMail=#{userMail},
</if>
<if test= "address!=null">
address=#{address},
</if>
<if test= "reason!=null">
reason=#{reason},
</if>
userId=#{userId} where userId=#{userId}
</update>
<select id="getList" parameterType="com.model.UserInfo" resultType="com.model.UserInfo">
select * from UserInfo where 1=1
<if test= "userName!=null">
And userName=#{userName}
</if>
<if test= "userPwd!=null">
And userPwd=#{userPwd}
</if>
<if test= "userMail!=null">
And userMail=#{userMail}
</if>
<if test= "address!=null">
And address=#{address}
</if>
<if test= "reason!=null">
And reason=#{reason}
</if>
<if test= "userId>0">
And userId=#{userId}
</if>
</select>
<select id="getListByPage" parameterType="com.model.UserInfo" resultType="com.model.UserInfo">
Select u.*
From (Select rownum as num,*
from userInfo Where 1=1
<if test= "userName!=null">
And userName=#{userName}
</if>
<if test= "userPwd!=null">
And userPwd=#{userPwd}
</if>
<if test= "userMail!=null">
And userMail=#{userMail}
</if>
<if test= "address!=null">
And address=#{address}
</if>
<if test= "reason!=null">
And reason=#{reason}
</if>
<if test= "userId>0">
And userId=#{userId}
</if>
) u Where u.num between (#{pageIndex}-1)*#{pageSize} and #{pageIndex}*#{pageSize}
</select>
<select id="getListByPages" parameterType="com.model.UserInfoPage" resultType="com.model.UserInfo">
select u.*
From (Select rownum as num,userInfo.*
from userInfo
<where>
<if test= "userName!=null">
And userName=#{userName}
</if>
<if test= "userPwd!=null">
And userPwd=#{userPwd}
</if>
<if test= "userMail!=null">
And userMail=#{userMail}
</if>
<if test= "address!=null">
And address=#{address}
</if>
<if test= "reason!=null">
And reason=#{reason}
</if>
<if test= "userId>0">
And userId=#{userId}
</if>
</where>)u
Where u.num between #{startNum} and #{endNum}
</select> </mapper>
5.编写相应的dao层。比如创建的是UserInfoMapper。截图如下:(这个层里全是接口)
<!--配置dao层 -->
6.编写相应的Service层,sevice层里需要引入的是dao层。比如:
package com.service;
import java.util.List;
import com.dao.imp.UserMapper;
import com.model.UserInfo;
public class UserInfoService {
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public List<UserInfo> getList(UserInfo u){
return userMapper.getList(u);
}
}
7.编写相应的action层:action层里需要service层:
package com.action;import java.util.List;
import com.model.UserInfo;
import com.service.UserInfoService;
public class UserInfoAction {
private UserInfoService userInfoService;
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public List<UserInfo> getList(UserInfo u){
return userInfoService.getList(u);
}
}
8.在spring里的配置文件,将各层注入到spring中。
<!--配置dao层 --><bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dao.imp.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 配置Service层 -->
<bean id="userInfoService" class="com.service.UserInfoService">
<property name="userMapper" ref="userMapper"></property>
</bean>
<!-- 配置action层 -->
<bean id="userInfoAction" class="com.action.UserInfoAction">
<property name="userInfoService" ref="userInfoService"></property>
</bean>