SSM整合--增删改查

来源:互联网 发布:淘宝卖家订单管理 编辑:程序博客网 时间:2024/04/30 05:33

5.1过去了,经过几天的尝试终于把SSM整合起来了。下面吧主要的配置说下,然后把源码上传上来。

 项目包结构:


配置文件我分成了spring-mvc.xml和spring-mybatis.xml;

spring-mvc.xml是spring mvc的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
    <!-- 扫描文件(自动注入),包括DAO层注入Service层,Service层注入Controller层 -->
    <context:component-scan base-package="com.demo"/>
    <!-- 默认的注解映射的支持 -->  
    <mvc:annotation-driven />
   <!-- 避免IE在ajax请求时,返回json出现下载 -->
   <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">     
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 静态资源(js/image)的访问 -->
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/Ext/**" location="/Ext/" />
     
   <!-- 对模型视图添加前后缀 -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>

spring-mybatis.xml是spring和mybatis整合的配置和事务管理的配置;数据源的配置我放在了tomcat中

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
<!--本示例采用DBCP连接池。 连接池配置如下 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/user" />
</bean>


<!-- mybatis文件配置,扫描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:conf/mybatis-config.xml"
p:mapperLocations="classpath:mapper/*.xml" /><!-- configLocation为mybatis属性 
mapperLocations为所有mapper -->


<!-- spring与mybatis整合配置,扫描所有dao ,生成与DAO类相同名字的bean(除了首字母小写)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.demo.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />


<bean id="userService" 
class="com.demo.service.impl.UserServiceImpl">
</bean>


<!-- 对数据源进行事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<tx:annotation-driven mode="proxy" transaction-manager="transactionManager" />
</beans>

在controller中有登录、增、删、改、查主要的方法就不一一介绍了下面会把源码上传的,有兴趣的可以看看。

package com.demo.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;


import net.sf.json.JSONObject;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.demo.model.User;
import com.demo.service.UserService;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;


/**
 * @Author Aaron
 * @Date 创建时间:2015-12-10
 * @Version 1.0
 * 
 * @Project_Package_Description springmvc || com.demo.controller
 * @Function_Description 核心控制类,处理页面的请求以及业务
 * 
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {


@Autowired
private UserService userService;


@RequestMapping(value = "/index")
public ModelAndView index(User user) {
userService.insertUser(user);
ModelAndView mav = new ModelAndView();
mav.setViewName("info");
mav.addObject("user", user);
return mav;
}


@RequestMapping(value = "/login")
public ModelAndView login(String name, String password,
HttpServletRequest request) {
User user = new User();
ModelAndView modelAndView = new ModelAndView();
user = userService.findByNameAndPassword(name, password);
String msg = null;


if (user != null) {
msg = "登录成功";
modelAndView.setViewName("redirect:/user/findallusers");
request.getSession().setAttribute("user", user);
} else {
msg = "输入的用户名或密码不正确";
modelAndView.setViewName("login");
modelAndView.addObject("msg", msg);
}


return modelAndView;
}


@RequestMapping(value = "/extlogin")
public Object extlogin(String name, String password,
HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
String randCode = request.getParameter("randCode");
User user = new User();
ModelAndView modelAndView = new ModelAndView();
user = userService.findByNameAndPassword(name, password);
String msg = null;
// 获得的当前正确的验证码
String rand = (String) request.getSession().getAttribute("rand");
if (rand.equals(randCode)) {
if (user != null) {
msg = "登录成功";
modelAndView.setViewName("redirect:/user/findallusers");
request.getSession().setAttribute("user", user);

} else {
msg = "输入的用户名或密码不正确";
modelAndView.setViewName("login");
}


}
map.put("msg", msg);
JSONObject jsonObject = JSONObject.fromObject(map);
return jsonObject;
}


@RequestMapping(value = "/findallusers")
public ModelAndView findAllUsers(HttpServletRequest request) {
List<User> users = new ArrayList<User>();
ModelAndView modelAndView = new ModelAndView();
users = userService.findAllUser();
modelAndView.setViewName("listuser");
modelAndView.addObject("users", users);
modelAndView.addObject("user", request.getSession()
.getAttribute("user"));
return modelAndView;
}


@RequestMapping(value = "/getUser")
public ModelAndView getUser(int id) {
User user = new User();
ModelAndView modelAndView = new ModelAndView();
user = userService.findById(id);
modelAndView.setViewName("edituser");
modelAndView.addObject("user", user);
return modelAndView;
}


@RequestMapping(value = "/delUser")
public ModelAndView delUser(int id) {
ModelAndView modelAndView = new ModelAndView();
userService.deleteUser(id);
modelAndView.setViewName("redirect:/user/findallusers");
return modelAndView;
}


@RequestMapping(value = "/updateUser")
public ModelAndView updateUser(User user) {
ModelAndView modelAndView = new ModelAndView();
userService.updateUser(user);
modelAndView.setViewName("redirect:/user/findallusers");
return modelAndView;
}


@RequestMapping(value = "/toaddUser")
public ModelAndView toaddUser() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("adduser");
return modelAndView;
}


@RequestMapping(value = "/addUser")
public ModelAndView addUser(User user) {
ModelAndView modelAndView = new ModelAndView();
userService.insertUser(user);
modelAndView.setViewName("redirect:/user/findallusers");
return modelAndView;
}


}

tomcat数据源配置

conf中的context.xml中增加

<Resource
   name="jdbc/user"
   auth="Container"
   type="javax.sql.DataSource"
   maxActive="100"
   maxIdle="30"
   maxWait="10000"
   driverClassName="com.mysql.jdbc.Driver"
   username="root"
   password=""
   url="jdbc:mysql://127.0.0.1:3306/mysql"/>

项目中的web增加

<resource-ref>
     <description>jdbc_user</description>
      <res-ref-name>jdbc/user</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

datasoure配置

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/user" />
</bean>

源码下载地址:http://download.csdn.net/detail/baidu_30438243/9511962


1 1
原创粉丝点击