1 将applocationContext文件拆分:各干各的事(调用时使用:Spring*)
1 Spring-db.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置JDBC -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.userName}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 读取配置文件 -->
<context:property-placeholder location="classpath:JDBC.properties"/>
</beans>
2 Spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这些所有可以在帮助文档中通过搜索 beans.xsd 复制而得
使用注解 需要导入
xmlns:context="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
-->
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis主配置文件位置 -->
<property name="configLocation" value="classpath:mybatisConfig.xml"></property>
<!-- 数据源连接池 使用c3p0 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 扫描指定包下的所有Dao接口,自动生成该接口的实现类 -->
<bean id="mapperScannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.woniu.Dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
3 Spring-service.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<bean id="userservice" class="com.woniu.serviceimp.UserServiceimp">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
4 Spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.woniu.conroller"/>
<!-- mvc注解驱动:自动创建框架底层需要的类的对象 -->
<mvc:annotation-driven />
</beans>
5 mybatisConfig.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>
<!-- 读取配置文件中的数据库信息 -->
<properties resource="JDBC.properties"/>
<typeAliases>
<!-- <typeAlias type="Entity.Cgoods" alias="Cgoods"/> --><!-- 指定类名作为全限定名 -->
<package name="com.woniu.entity"/> <!-- 指定包中所有类都使用类名代替全限定名 -->
</typeAliases>
<!-- JDBC4大参数,数据源,连接池 全部交给Sping来做 -->
<mappers>
<!-- 管理com.woniu.Dao 下的所有映射文件 -->
<package name="com.woniu.Daoimp"/>
</mappers>
</configuration>
6 JDBC.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///bank
jdbc.userName=root
jdbc.password=123456
2 web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring-myssm</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>
<!-- 查找Spring-mvc文件 -->
<servlet>
<servlet-name>spring_mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring_mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 通过过滤器设置编码格式 -->
<filter>
<filter-name>my_filter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>my_filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3 所需jar包
4 项目层次
5 controller 处理各种请求
package com.woniu.conroller;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.entity.ResponseResult;
import com.woniu.entity.Users;
import com.woniu.service.UserService;
@Controller
public class Mycontroller {
UserService userService;
@RequestMapping("/tofirst.do")
public ModelAndView tofirst(Users users) {
all_userService().addUser(users);
ModelAndView ve =new ModelAndView();
ve.addObject("qwe", "添加成功");
ve.setViewName("/a.jsp");
return ve ;
}
//同步请求 使用ModelAndView
@RequestMapping("/select_All_Users.do")
public ModelAndView select_All_Users() {
List<Users> users = all_userService().select_users();
ModelAndView ve =new ModelAndView();
ve.addObject("users", users);
ve.setViewName("/a.jsp");
return ve ;
}
//同步请求 使用String
@RequestMapping("/select_All_Users1.do")
public String select_All_Users1(HttpServletRequest req,HttpServletResponse res) {
List<Users> users = all_userService().select_users();
req.setAttribute("users", users);
return "/a.jsp";
}
//异步请求 自行采用json
@RequestMapping("/yibu.do")
public void yibu(HttpServletRequest req,HttpServletResponse res) throws JsonGenerationException, JsonMappingException, IOException {
List<Users> users = all_userService().select_users();
ObjectMapper obm=new ObjectMapper();
obm.writeValue(res.getOutputStream(), users);
}
//异步请求 采用MVC注解
@ResponseBody
@RequestMapping("/yibu1.do")
public ResponseResult<List<Users>> yibu1(HttpServletRequest req,HttpServletResponse res) throws JsonGenerationException, JsonMappingException, IOException {
ResponseResult<List<Users>> result=new ResponseResult<List<Users>>(200, all_userService().select_users(), "success");
return result;
}
public UserService all_userService() {
ApplicationContext ac=new ClassPathXmlApplicationContext("Spring-*");
userService = (UserService) ac.getBean("userservice");
return userService;
}
}
6 同步异步请求的.jsp
1 同步请求
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.5.1.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<c:forEach var="i" items="${users}">
${i.uname}<br/>
</c:forEach>
</body>
</html>
2 异步请求
1 注解版
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.5.1.js"></script>
<script type="text/javascript">
$(function() {
$.post("<c:url value='/yibu1.do'/>",null,
function(data) {
console.log(data);
for(i=0;data.data.length;i++){
alert(data.data[i].uname);
}
});
})
</script>
</head>
<body>
c
</body>
</html>
2 自行使用jacson 版
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.5.1.js"></script>
<script type="text/javascript">
$(function() {
$.post("<c:url value='/yibu.do'/>",null,
function(data) {
console.log(data);
for(var i=0;i<data.length;i++){
console.log(data[i]);
}
},"json");
})
</script>
</head>
<body>
</body>
</html>