spring+mybaties整合及三层的架构(不是完整版)

本文详细介绍如何将Spring框架与MyBatis进行整合,包括引入依赖、配置数据源、搭建三层架构、编写实体类及SQL映射文件等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringMybatis的整合

1.引入相应的jar包。(Mybatisjar包,Springjar包,mybatis-spring-1.1.1.jar)。



2.编写相应的包(搭建3层)

3.配置相应的spring的配置。

 1)在applicationContext里配置相应的数据源的配置。

<?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>

2)配置mybatisSqlSessionFactory(也需要在ApplicationContext里做配置)。

<p>
</p><p>    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  </p><p>      <property name="dataSource" ref="jdbcDataSource" />  </p><p>      <property name="configLocation" value="classpath:mybatis-config.xml"></property>  </p><p>   </bean></p>

4.搭建mybatis的框架了(编写相应的实体类,SQL映射文件)。结构如下:



注意的问题:去掉<association>节点里的foreignColumn属性。

            去掉所有的select节点里的resultSets的属性。

比如之前设置的UserInfoMapper

现在以UserInfo为例

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.gxa.bj.dao.UserInfoMapper">
   <insert id="getInsert" parameterType="com.gxa.bj.model.UserInfo">
       insert into UserInfo(userid,username,userpwd,flag)
       values(usernext.nextval,#{userName},#{userPwd},#{flag})
   </insert>
   <delete id="getDelete">
        delete from UserInfo where userId=#{userId}
   </delete>
   <update id="getUpdate" parameterType="com.gxa.bj.model.UserInfo">
   		update UserInfo set username=#{userName},userpwd=#{userPwd},flag=#{flag} 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">
   		select * From UserInfo Where userName like '%${value}%'
   </select>
      <select id="getList"  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>
</mapper>


5.编写相应的dao层。比如创建的是UserInfoMapper。截图如下:(这个层里全是接口)

package com.gxa.bj.dao;

import java.util.List;

import com.gxa.bj.daoimp.IDaoimp;
import com.gxa.bj.model.UserInfo;

public interface UserInfoMapper extends IDaoimp<UserInfo>{
	public List<UserInfo> getList();
}


6.编写相应的Service层,sevice层里需要引入的是dao层。

package com.gxa.bj.service;

import java.util.List;

import com.gxa.bj.dao.UserInfoMapper;
import com.gxa.bj.model.UserInfo;

public class UserInfoService {
	//有dao层的对象为字段
	private UserInfoMapper userInfoMapper;
	
	public UserInfoMapper getUserInfoMapper() {
		return userInfoMapper;
	}

	public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
		this.userInfoMapper = userInfoMapper;
	}
	//查询多条数据方法
	public List<UserInfo> getList(){
		return userInfoMapper.getList();
	}
}


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 {
	//有service层的对象作为字段
	private UserInfoService userInfoService;

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	public List<UserInfo> getList(){
		return userInfoService.getList();
	}
}

8.spring里的配置文件,将各层注入到spring中。

<!-- 配置Dao层 -->
	<bean id="userInfoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		 <property name="mapperInterface" value="com.gxa.bj.dao.UserInfoMapper"></property>
		 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	<!-- 配置Service层 -->
  	<bean id="userInfoService" class="com.gxa.bj.service.UserInfoService">
  		<property name="userInfoMapper" ref="userInfoMapper"></property>
  	</bean>
  	<!-- 配置Action层 -->
  	<bean id="userInfoAction" class="com.gxa.bj.action.UserInfoAction">
  		<property name="userInfoService" ref="userInfoService"></property>
  	</bean>

在mybaties-config.xml里配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <mappers>
    <mapper resource="com/gxa/bj/model/UserInfoMapper.xml"/>
  </mappers>

</configuration>

9.完整的applicationContext.xml文件配置如下:


  
    
	
		
			oracle.jdbc.driver.OracleDriver
		
		
			jdbc:oracle:thin:@127.0.0.1:1521:orcl
		
		
			bbs
		
		
			123
		
	
	
	   
        
        
   
	
	
		 
		 
	
	
  	
  		
  	
  	
  	
  		
  	

补充,如果改用C3P0作为数据源:

第一步,需要引入C3P0jar包:


第二步,将之前的C3P0的配置改成spring中的属性注入。

比如之前的C3P0的配置如下:

c3p0.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
c3p0.driverClass=oracle.jdbc.driver.OracleDriver
c3p0.user=bbs
c3p0.password=123
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60   
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60  
c3p0.maxPoolSize=20
c3p0.maxStatements=100 
c3p0.minPoolSize=5


第三步,改成spring中的注入方式:

<!-- 配置数据源-->
	<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>bbs</value>
		</property>
		<property name="password">
			<value>123</value>
		</property>
		<property name="initialPoolSize">
		    <value>10</value>
		</property>
	</bean>  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值