关于Spring和mybatis的整合
Spring同Mybatis的整合
1.引入相应的jar包。(Mybatis的jar包,Spring的jar包,mybatis-spring-1.1.1.jar)。
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="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<property name="username">
<value>bbs</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
</beans>
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映射文件)。结果如下:
注意的问题:去掉<association>节点里的foreignColumn属性。
去掉所有的select节点里的resultSets的属性。
比如之前设置的UserInfoMapper。
<?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.gxa.bj.dao.imp.UserMapper">
<insert id="addItem" parameterType="com.gxa.bj.model.UserInfo">
insert into UserInfo(userid,username,userpwd,useremail,useraddress,regreason)
values(usernext.nextval,#{userName},#{userPwd},#{userEmail},#{userAddress},#{regReason})
</insert>
<delete id="removeItem">
delete from UserInfo where userId=#{id}
</delete>
<update id="updateItem" parameterType="com.gxa.bj.model.UserInfo">
update userinfo set
<if test="userName!=null">
userName = #{userName},
</if>
<if test="userPwd!=null">
userPwd = #{userPwd},
</if>
<if test= "userEmail!=null">
userEmail = #{userEmail},
</if>
<if test= "userAddress!=null">
userAddress = #{userAddress},
</if>
<if test= "regReason!=null">
regReason = #{regReason},
</if>
userId=#{userId} Where userId=#{userId}
</update>
<select id="getModel" resultType="com.gxa.bj.model.UserInfo">
select * from userinfo where userid=#{id}
</select>
<select id="getUsers" parameterType="java.lang.String" resultType="com.gxa.bj.model.UserInfo" resultSets="com.gxa.bj.model.UserInfo">
select * from userinfo where userName like '%${value}%'
</select>
<select id="getList" resultSets="com.gxa.bj.model.UserInfo" parameterType="com.gxa.bj.model.UserInfo" resultType="com.gxa.bj.model.UserInfo">
Select * From userInfo
<where>
<if test="userName!=null">
And userName like #{userName}
</if>
<if test="userId>0">
And userId =#{userId}
</if>
<if test="userPwd!=null and userPwd!=''">
And userPwd like #{userPwd}
</if>
</where>
</select>
<select id="getListByPage" parameterType="com.gxa.bj.model.UserInfoPage" resultType="com.gxa.bj.model.UserInfo">
Select u.*
From (Select rownum as num, userinfo.*
from userinfo
<where>
<if test="userName!=null">
And userName like #{userName}
</if>
<if test="userId >= 10">
And userId =#{userId}
</if>
<if test="userPwd!=null and userPwd !='' ">
And userPwd like #{userPwd}
</if>
</where>
) u Where u.num between #{startNum} and #{endNum}
</select>
</mapper>
5.编写相应的dao层。比如创建的是UserInfoMapper。截图如下:(这个层里全是接口)
6.编写相应的Service层,sevice层里需要引入的是dao层。
package com.gxa.bj.service;
import java.util.List;
import com.gxa.bj.dao.imp.UserMapper;
import com.gxa.bj.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 t){
return userMapper.getList(t);
}
}
7.编写相应的action层:action层里需要service层:
package com.gxa.bj.action;
import java.util.List;
import com.gxa.bj.model.UserInfo;
import com.gxa.bj.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.gxa.bj.dao.imp.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 配置Service层 -->
<bean id="userInfoService" class="com.gxa.bj.service.UserInfoService">
<property name="userMapper" ref="userMapper"></property>
</bean>
<!-- 配置Action层 -->
<bean id="userInfoAction" class="com.gxa.bj.action.UserInfoAction">
<property name="userInfoService" ref="userInfoService"></property>
</bean>
关于Spring和mybatis的整合的更多相关文章
- 基于maven进行spring 和mybatis的整合(Myeclpise)
学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试 ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
写在前面的话 承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复
写在前面的话 在测试搜索时出现的问题,mysql通过中文查询条件搜索不出数据,但是英文和数字可以搜索到记录,中文无返回记录.本文就是写一下发现问题的过程及解决方法.此bug在第一个项目中点这里还存在, ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十一)SVN服务器进阶
日常啰嗦 上一篇文章<Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建>简单的讲了一下SVN服务器的搭建,并没有详细的介绍配置文件及一些复杂的功能, ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十二)阶段总结
不知不觉,已经到了基础篇的收尾阶段了,看着前面的十几篇文章,真的有点不敢相信,自己竟然真的坚持了下来,虽然过程中也有过懒散和焦虑,不过结果还是自己所希望的,克服了很多的问题,将自己的作品展现出来,也发 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(二)Log4j讲解与整合
日常啰嗦 上一篇文章主要讲述了一下syso和Log间的一些区别与比较,重点是在项目的日志功能上,因此,承接前文<Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)Sy ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例
日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试>讲了不为和不能两个状态,针对不为,只能自己调整心态了,而对于不能,本文会结合一 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(五)结合MockMvc进行服务端的单元测试
日常啰嗦 承接前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例>,已经讲解了dao层和service层的单元测试,还有控制器这层也不能 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能
日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合>讲了富文本编辑器UEditor的整合与使用 ...
随机推荐
- Python: 元组的基本用法
元组和列表是近亲,列表允许并且提供了方法来改变它的值,但元组是不可改变的,即不允许你改变它的值----这也是它没有方法的部分原因. 元组的主要作用是作为参数传递给函数调用.或是从函数调用那里获得参数时 ...
- Jquery--弹窗
<title>弹窗</title> <script src="JS/jquery-1.7.2.js"></script> <s ...
- 9.3 js基础总结3
2.后增量/后减量运算符 ++,-- var i = 10; var a = i++; // i = i + 1; alert(a); 3.比较运算符(>,<,>=,<=,== ...
- 跨过几个坑,终于完成了我的第一个Xamarin Android App!
时间过得真快,距离上次发随笔又是一年多.作为上次发的我的第一个WP8.1应用总结的后继,这次同样的主要功能,改为实现安卓版APP.前几个月巨硬收购Xamarin,把Xamarin集成到VS里了,大大方 ...
- VFP自定义函数StringFormat (仿.NET String.Format 方法)
VFP仿.NET String.Format 方法 将指定字符串中的每个{x}替换为相应值,并返回文本 *-- 调用格式 StringFormat("日期{2},字符{1}",&q ...
- Nginx的nginx.conf配置文件中文注释说明
#运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 erro ...
- sqlserver创建,调用 带返回值存取过程
<1>create: ALTER proc [dbo].[common_proc] @sql1 varchar(5000), @sql2 varchar(5000) OUTPUT as ...
- python生成透时图片and 写文字
import Image from get_png import getpng def transparent(infile): #open png,covert it into 'RGBA mode ...
- Winform GDI+ 相关资料
在Visual Studio 2010中定义GDI+自定义控件——自定义控件介绍 http://www.cnblogs.com/zhangdong/archive/2010/05/20/1740177 ...
- C#中常用的几种读取XML文件的方法
1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/