关于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>

时间: 2024-08-08 05:36:33

关于Spring和mybatis的整合的相关文章

SSM框架Spring+SpringMVC+MyBatis——详细整合教程

摘要: 包括SQL Maps和Data Access ObjectsDAOMyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的... 摘要:   spring MVC属于SpringFrameWork的后续产品已经融合在Spring Web Flow里面.Spring MVC 分离了控制器.模型对... 1.基本概念 1.1.Spring Spring是一个开源框架Spring是于2003 年兴起的一个轻量级的Java 开发框架由Rod Johnson 在其著作Expert 

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)

spring,springmvc,mybatis基本整合(一)–xml文件配置方式(2)之mapper接口 一,整合结构 二,所需jar包 如上图. 三,整合配置 1,web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://j

Spring和MyBatis环境整合【转】

Spring和MyBatis环境整合[转] SSH框架的结合几乎家喻户晓,但是一般的中小项目,使用Spring和MyBatis就够了,而且MyBatis轻便好使,易上手,值得大家尝试一次. 开篇简介: Spring: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. 两个重要模块:Spring 面向方面编程(AOP)和控制反转 (IOC) 容器. 控制反转模式(也称作依赖性介入)的基本概念是:不创建对象,但是描述创建它们的方式.在代码中不直接与对象和服务连接,但在配置

基于maven进行spring 和mybatis的整合(Myeclpise)

学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试.发布和报告等.在大型项目开发中,使用maven来管理是必不可少的. 开发工具myeclipse:myclipse 10.0自带了maven的插件.也可以网上下载maven插件 1.新建一个maven project,点击next,选择maven-archetype-webapp,点击next,填写

学习笔记——Spring+SpringMVC+MyBatis框架整合

一.Maven创建项目 1. 在Eclipse中选择New -> Project -> Maven -> Maven Project 2. 选择默认workspace之后建立maven-webapp 3. 填写Group Id和Artifact Id(项目名称) 4. 建立工程后发现目录结构报错 5. 为了避免乱码,右键点击工程选择Properties -> Resource,选择编码方式为UTF-8 6. 在Properties中选择Java Build Path -> J

spring+SpringMVC+Mybatis框架整合

使用SSM(spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些.以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下.这次,先说说三大框架整合过程.个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助.不过,如果用都不会,谈思想就

(Spring+SpringMVC+MyBatis)整合教程

此文章是转载来的,点击最后的链接下载代码,经博主测试已经能够跑起来,注意的坑点如下: 1.这个项目为maven项目,下载好项目后,在eclipse上右键->import->maven->Existing maven projects,这样导入项目会自动加载需要的jar包,前提是,eclipse已经装好并配置好maven. 2.部署到tomcat上的时候,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包, 你需要设置一下eclipse: 项目 -> 属性 -&g

日常开发系列——Maven+Spring+Spring MVC+MyBatis+MySQL整合SSM框架

进入公司开发已经3个多月了,项目用的是Maven+Spring+Spring MVC+MyBatis+MySQL,趁这个周末有空,仔细研读一下公司项目的基本框架,学习一下这个环境是怎么搭建起来的,经过自己的研究终于是成功地试验出来.自己亲手做的才算是自己学到的,决定将其记录下来,以便日后查询,源码同时也欢迎大家拍砖. 一.数据库的准备 这次整合试验想着做个简单的,就决定做一个普通的用户登陆,就一张表吧 我新建的数据库名字是test,然后新建了一张表 DROP TABLE IF EXISTS `u

Spring 与 MyBatis 的整合

本文讨论 Spring 与 MyBatis 的整合. 在 beans.xml 中我们定义了两个 bean: SqlSessionFactoryBean.SqlSessionTemplate. 1.SqlSessionFactoryBean 是 FactoryBean,它在 Spring 容器中返回 SqlSessionFactory bean. a.单例模式,通过 getObject() 返回,源码如下: 可以看出,SqlSessionFactoryBean 对 sqlSessionFactor