spring +struts2 + ibatis + mysql 案例配置

前几天写了Mybaits的案例,今天上传一个ibatis的案例,只有查询,删除和添加修改,都有插入代码,注释了,根据查询再去做,开发方便action没有交给spring管理,前辈们见笑了

第一步:案例结构  (sql数据库在   sql文件夹里面)

 

第二步:需要的jar包

 

第三步:配置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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 设置字符编码过滤器 -->
  <filter>
   <filter-name>CharacterEncodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>CharacterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <!-- struts2过滤器 -->
  <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>*.do</url-pattern>
  </filter-mapping>
 
   <!-- spring的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  <!-- 得到spring配置文件的路径 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- 为什么是 /WEB-INF/classes  项目空间显示方式为Navigator就可以看见这个路径
     还可以classpath*:applicationContext.xml
     主配置文件中都是/WEB-INF/classes/这种
    -->
    <param-value>
     /WEB-INF/classes/applicationContext.xml
    </param-value>
  </context-param>
</web-app>

 

第四步:配置src下的applicationContext.xml

 

<?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:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 引入jdbc配置文件 -->
 <context:property-placeholder
  location="/WEB-INF/classes/mysql.properties" />
  
 <!--  配置dbcp数据源 -->
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${driverName}"></property>
  <property name="url" value="${url}"></property>
  <property name="username" value="${username}"></property>
  <property name="password" value="${password}"></property>
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="30"></property>
  <!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用不到的连接释放,一直减少到maxIdle为止 -->
  <property name="maxIdle" value="10"></property>
  <!-- 当最小空闲时,当连接少于minIdle时会自动去申请一些连接 -->
  <property name="minIdle" value="5"></property>
  <!-- 最大等待时间 5秒钟-->
  <property name="maxWait" value="5000"></property>
 </bean>
 
 <!-- 配置事务管理 -->
 <bean id="txManage"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <!-- 配置哪些方法要加入事务控制 -->
 <bean id="txIntercept"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="txManage" />
  <property name="transactionAttributes">
   <props>
    <prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED, readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED, -Exception</prop>
   </props>
  </property>
 </bean>
 <!-- 哪个类中的哪些方法要加入事务处理 -->
 <bean id="autoProxy"
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>
    <value>*Service</value>
   </list>
  </property>
  <property name="interceptorNames">
   <list>
    <value>txIntercept</value>
   </list>
  </property>
 </bean>
 
 
 <!-- 配置SqlMapClient工厂 -->
 <bean id="sqlMapClient"
  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <!-- Ibatis的配置文件 -->
  <property name="configLocation"
   value="/WEB-INF/classes/SqlMapConfig.xml">
  </property>
  <!-- 引入数据源 -->
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <!-- 公用CommonDao -->
 <bean id="commonDao" class="com.bao.dao.CommonDaoImpl">
  <property name="sqlMapClient" ref="sqlMapClient"/>
 </bean>
 
 <!-- 用户业务层 -->
 <bean id="userInfoServices"
   class="com.bao.services.UserInfoServicesImpl">
  <property name="commonDao" ref="commonDao"/>
 </bean>
 
</beans>

第五步:配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <!-- .do配置 -->
    <constant name="struts.action.extension" value="do" />
    <!-- 是否為開發模式 -->
    <constant name="struts.devMode" value="false" />
 <package name="default" extends="struts-default" namespace="/">
  <action name="userManage_*" method="{1}"
   class="com.bao.action.UserInfoAction">
   <result name="list">
    /WEB-INF/jsp/user_list.jsp
   </result>
  </action>
 </package>
 
</struts>

 

 

第六步:配置jdbc连接数据库字符 (根据自己的数据库去改)

driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=1234

 

第七步:配置SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
     
<sqlMapConfig>
 
 <settings cacheModelsEnabled="true" enhancementEnabled="true"
  lazyLoadingEnabled="true" maxRequests="64" maxSessions="20"
  maxTransactions="10" useStatementNamespaces="true" />
  <!-- 配置 useStatementNamespaces="true":意思就是可以根据    实体的xml的命名空间.方法-->
  
  <!-- 用户sqlMap -->
  <sqlMap resource="com/bao/model/sqlmap_user.xml"/>
</sqlMapConfig>

 

第八步:配置sqlmap_user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="user">
 <!-- 类别名 -->
 <typeAlias alias="acc"
  type="com.bao.model.UserInfo" />
 <typeAlias alias="dep"
  type="com.bao.model.Dep" />
  
  <!-- 映射  column 表示数据库字段    property类中的字段 -->
 <resultMap class="acc" id="accMap">
  <result column="userId" property="userId" />
  <!-- 由于这里在数据库中是字段,而类中是对象,所以在映射的时候,还要根据id获取对象
   user.getDep:  调用的namespace="user" 这个xml 中的getDep方法
  -->
  <result column="depId" property="dep"
   select="user.getDep" />
  <result column="userName" property="userName" />
  <result column="userPwd" property="userPwd"/>
 </resultMap>
 
 <select id="getAcc" resultMap="accMap">
  select * from userInfo
  <dynamic prepend="where">
   <isNotEmpty prepend="and" property="users.userId">
    userId=#users.userId#
   </isNotEmpty>
   <isNotEmpty prepend="and" property="users.account">
    userName like concat('%', #users.userName#, '%')
   </isNotEmpty>
  </dynamic>
 </select>
 
 <select id="getDep" resultClass="dep">
  select * from dep where depId=#value#
 </select>
 </sqlMap>

 

第九步:entity配置(get和set方法和数据库一致,主外键可以写对象)

 

第十步:控制层UserInfoAction (BasicAction是自己写的一个基础action继承了struts2的ActionSupport)

package com.bao.action;

import java.sql.SQLException;
import java.util.List;

import com.bao.model.UserInfo;
import com.bao.services.UserInfoServices;

public class UserInfoAction extends BasicAction{

 private static final long serialVersionUID = -1927230658317689227L;
 
 private UserInfoServices userInfoServices; //spring所管理的业务层,必须与配置的一致
 private List<UserInfo> listUser;
 private UserInfo user;
 
 public String listAcc() throws SQLException{
  listUser = userInfoServices.getlistUser(user);
  return "list";
 }

 public List<UserInfo> getListUser() {
  return listUser;
 }

 public void setUserInfoServices(UserInfoServices userInfoServices) {
  this.userInfoServices = userInfoServices;
 }
 
}

第十一步:公用dao

 

接口:

 

package com.bao.dao;

import java.sql.SQLException;
import java.util.List;

@SuppressWarnings("unchecked")
public interface CommonDao {
 
 /**
  * 获取返回单个值
  */
 Object queryObject(String sqlId, Object parameter)throws SQLException;
 
 /**
  * 获取集合对象
  */
     List queryList(String sqlId, Object parameter) throws SQLException;
 /**
  * 添加
  */
  Object addObj(String sqlId, Object parameter) throws SQLException;
 /**
  * 修改
  */
  int editObj(String sqlId, Object parameter) throws SQLException;
 /**
  * 删除
  */
  int del(String sqlId, Object parameter) throws SQLException;
 
}

实现类:

package com.bao.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
@SuppressWarnings("unchecked")
public class CommonDaoImpl extends SqlMapClientTemplate implements CommonDao {

 @Override
 public Object queryObject(String id, Object Paramter) throws SQLException {
  return getSqlMapClient().queryForObject(id, Paramter);
 }

 @Override
 public List queryList(String sqlId, Object parameter) throws SQLException {
  return  getSqlMapClient().queryForList(sqlId,parameter);
 }

 @Override
 public Object addObj(String sqlId, Object parameter) throws SQLException {
  return getSqlMapClient().insert(sqlId, parameter);
 }

 @Override
 public int del(String sqlId, Object parameter) throws SQLException {
  return getSqlMapClient().delete(sqlId, parameter);
 }

 @Override
 public int editObj(String sqlId, Object parameter) throws SQLException {
  return getSqlMapClient().update(sqlId, parameter);
 }

}

 

第十二步:services

接口:

package com.bao.services;

import java.sql.SQLException;
import java.util.List;

import com.bao.model.UserInfo;

public interface UserInfoServices {
 /**
  * 根据条件获取用户
  * @param user
  * @return
  */
 List<UserInfo> getlistUser(UserInfo user) throws SQLException;

}

实现类:

package com.bao.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bao.dao.CommonDao;
import com.bao.model.UserInfo;


/**
 * 业务层实现类
 * @author Administrator
 *
 */
public class UserInfoServicesImpl implements UserInfoServices {
 
 private CommonDao commonDao;

 public void setCommonDao(CommonDao commonDao) {
  this.commonDao = commonDao;
 }

 @Override
 public List<UserInfo> getlistUser(UserInfo user) throws SQLException {
  //查询的时候由于配置文件中是  对象.字段   所以我们要封装一次
  Map<String,Object> paraMap = new HashMap<String,Object>();
  //注意这里的users是xml文件中调用的对象名
  paraMap.put("users", user);
  //空间名.方法
  return commonDao.queryList("user.getAcc", paraMap);
 }
 
 
 
}

 

源码下载(10M)

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值