一.整合之后结构如下:
具体配置内容如下:
1.创建springmvc.xml文件配置如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<!-- 用注解的方式 ,代替配置注解映射器,配置注解适配器-->
<context:annotation-config>
<mvc:annotation-driven>
</mvc:annotation-driven>
</context:annotation-config>
<!-- 引入解析jstl的类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!-- 在实际开发中采取的是包的扫描,将该包扫描到spring容器下 -->
<context:component-scan base-package="com.gxa.bj.action"></context:component-scan>
</beans>
2.创建文件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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName"
>
<!-- 配置数据源,c3p0方式-->
<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>ErpSystem</value>
</property>
<property name="password">
<value>123</value>
</property>
<property name="initialPoolSize">
<value>10</value>
</property>
</bean>
<!-- 配置session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--(原)配置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> -->
<!-- mapper接口的扫描,必须扫描到接口,扫描的原则是:所有扫描进Spring的Mapper对象,它的命名规则:
首字母小写,后面的都是按照原有的接口名字定义。
比如UserMapper接口扫描到spring里,id名为userMapper
(现)扫描dao层
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gxa.bj.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (原)配置Service层 -->
<!-- <bean id="userInfoService" class="com.gxa.bj.service.UserInfoService">
<property name="userInfoMapper" ref="userInfoMapper"></property>
</bean> -->
<!-- (现)扫描Service层 -->
<context:component-scan base-package="com.gxa.bj.service"></context:component-scan>
<!-- 在spring中声明事务的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jdbcDataSource" />
</bean>
<!-- 事务增强的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 事务属性的配置,配置都哪些方法上 -->
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceCut" expression="execution(public * com.gxa.bj.service.*.*(..))" />
<aop:advisor pointcut-ref="serviceCut" advice-ref="txAdvice" />
</aop:config>
</beans>
3.创建文件mybatis-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>
4.web.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssmmodel1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc2</servlet-name>
<url-pattern>*.action</url-pattern><!--配置的访问路径,一定是按照这种格式写 -->
</servlet-mapping>
<!-- 需要把applicationContext.xml加入到随着web容器启动的时候就自己加载。使用监听配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
5.daoimp层里的IDao接口内容如下:
package com.gxa.bj.daoimp;
import java.util.List;
public interface IDaoimp<T> {
/**
* 向数据库表里添加一条数据的方法
*/
public int addItem(T t);
/**
* 从数据库表里删除一条数据的方法
* @param objId
*/
public int removeItem(Object objId);
/**
* 更新数据库表里的一条字段的方法
* @param t
*/
public int updateItem(T t);
/**
* 根据主键字段获取该条数据,并转换成实体对象
*/
public T getModel(Object objId) ;
/**
* 根据查询条件获取多条数据,并转换成相应的集合
*/
public List<T> getList(T t);
}
6.dao层里Mapper接口内容如下:
package com.gxa.bj.dao;
import com.gxa.bj.daoimp.IDaoimp;
import com.gxa.bj.model.UserInfo;
/**
* 继承了IDaoimp接口就拥有了它的方法,如果需要添加新方法,在下面添加
* @author lv
*
*/
public interface UserInfoMapper extends IDaoimp<UserInfo>{
7.model层里的实体类及sql映射xml文件内容如下:
1)userInfo实体类
package com.gxa.bj.model;
public class UserInfo {
private Integer userId;//用户编号
private String userName;//用户姓名
private String userPwd;//用户密码
private String flag;//标示
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
}
2)sql映射文件userInfoMapper.xml:
<?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=#{id}
</delete>
<update id="getUpdate" 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="flag!=null">
flag=#{flag},
</if>
userId=#{userId} Where userId=#{userId}
</update>
<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=#{userName}
</if>
<if test="userPwd!=null">
And userPwd=#{userPwd}
</if>
<if test="flag!=null">
And flag=#{flag}
</if>
<if test="userId>0">
And userId=#{userId}
</if>
</where>
</select>
<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>
</mapper>
8.Service层里的业务层内容如下:
加上@Service注解,才能吧Service扫描进Spring IOC容器
package com.gxa.bj.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.gxa.bj.dao.UserInfoMapper;
import com.gxa.bj.model.UserInfo;
@Service
public class UserInfoService {
private UserInfoMapper userInfoMapper;
public List<UserInfo> getList(UserInfo u){
return userInfoMapper.getList(u);
}
public UserInfoMapper getUserInfoMapper() {
return userInfoMapper;
}
public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
this.userInfoMapper = userInfoMapper;
}
public UserInfo getLogin(String name,String Pwd){
UserInfo u = new UserInfo();
u.setUserName(name);
u.setUserPwd(Pwd);
List<UserInfo> list=userInfoMapper.getList(u);
if(list.size()>0){
return list.get(0);
}else{
return null;
}
}
}
9.action层里的界面层内容如下:
Controller(控制器)主要负责处DispacherServlet分发的请求
定义i@Controller注解标记,然后使用@RequestMapping和@RequestParam等注解来定义url请求和方法直接的映射
package com.gxa.bj.action;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gxa.bj.model.UserInfo;
import com.gxa.bj.service.UserInfoService;
@Controller
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);
}
@RequestMapping(value="/getalluser.action")
public ModelAndView getAllUser(){
ModelAndView model = new ModelAndView();
List<UserInfo> list = userInfoService.getList(null);
model.addObject("users", list);
model.setViewName("/index.jsp");
return model;
}
@RequestMapping(value="/login.action")
public void login(HttpServletRequest req,HttpServletResponse resp){
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String userName=req.getParameter("userName");
String userPwd=req.getParameter("userPwd");
UserInfo u=userInfoService.getLogin(userName,userPwd);
if(u!=null){
System.out.println("登陆成功");
req.getSession().setAttribute("userInfo",u);
try {
req.getRequestDispatcher("login.jsp").forward(req, resp);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("登陆失败");
}
}
}