三大框架的总和

一个图
创建一个web项目
导入包,整合包,三十六个

新建项目包路径
在这里插入图片描述
导入配置文件
db.prperties 复制
log4i.prperties 复制

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 1.会话工厂 2.mapper代理bean 3.service 4.事务管理 5.数据源 -->
	
	
</beans>

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	
</beans>

怎样让:项目一启动,自己读取配置文件(以前都是在测试类中读取的)
(我们使用监听的方式可以监听request,session,context)
我们需要监听一个和项目同样生命周期的一个东西——也就是监听context

所以我们先写一个监听器在web.xml配置文件里
使用的是spring提供的监听器,(也可以自己创建)

  <!-- 加载spring配置文件的监听器,监听servletContext的创建 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

配置前端控制器和编码过滤器

	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  		<!-- 
  			*.do	以.do结尾的请求
  			/	处理所有请求
  			/* 	这是错误的写法
  		 -->
  	</servlet-mapping>  
  	<!-- 编码过滤器 -->
    <filter>
  		<filter-name>EncodingFilter</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>EncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>

总起来的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>ssm</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配置文件的监听器,监听servletContext的创建 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  		<!-- 
  			*.do	以.do结尾的请求
  			/	处理所有请求
  			/* 	这是错误的写法
  		 -->
  	</servlet-mapping>  
  	<!-- 编码过滤器 -->
    <filter>
  		<filter-name>EncodingFilter</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>EncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>
</web-app>

配置application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 1.会话工厂 2.mapper代理bean 3.service 4.事务管理 5.数据源 -->
	
	<!-- 1.加载外部资源文件 -->
	<context:property-placeholder location="db.properties"/>
	
	<!-- 2.使用c3p0数据库连接池 -->
	<bean id = "dataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value = "${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
		
	<!-- 3.会话工厂配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 起别名 -->
		<property name="typeAliasesPackage" value= "com.cbb.pojo"></property>
	</bean>
	
	<!-- 4.mapper代理bean -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定接口所在的包 -->
		<property name="basePackage" value="com.cbb.mapper"></property>
	</bean>
	
	<!-- 5.service bean -->
	<context:component-scan base-package="com.cbb.service"></context:component-scan>
	<!-- 6.spring事务管理:6.1配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 6.2 事务注解驱动 -->
	<tx:annotation-driven/>
</beans>

配置springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!--注解驱动-->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<context:component-scan base-package="com.cbb.controller"></context:component-scan>
	
	<!-- 处理静态资源被defaultservlet处理 -->
	<mvc:default-servlet-handler/>
	
</beans>

我们实现根据id查询信息
写User类

package com.cbb.pojo;

import java.util.Date;

/** 
 * 类描述:User实体类
 * 作者: shendong
 * 创建日期:2019年3月7日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */

public class User {
	private int id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
				+ address + "]";
	}
	public User(int id, String username, Date birthday, String sex, String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	
}

UserMapper.java接口类

package com.cbb.mapper;

import java.util.List;
import java.util.Map;

import com.cbb.pojo.User;

/** 
 * 类描述:
 * 作者: 地铁与人海
 * 创建日期:2019年3月20日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */

public interface UserMapper {

	/** 
	 * 方法描述:根据id查询
	 * @param id  用户id
	 * @return
	 */
	public User selectById(int id);
	
	/** 
	 * 方法描述:根据条件查找记录
	 * @param map
	 * @return
	 */
	public List<User> selectIf( Map<String,Object> map);
}

映射文件UserMapper.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.cbb.mapper.UserMapper">
	<select id="selectById" parameterType="int" resultType="User">
		select * from user where id = #{id}
	</select>
	
	<select id="selectIf" parameterType="map" resultType="User">
		select * from user
		<where>
			<if test="username!=null and username!='' ">
			and username=#{username}
			</if>
			<if test="sex!=null and sex!=''">
			and sex = #{sex}
			</if>
			
		</where>

	</select>
</mapper>

我们开发接口 UserService接口

package com.cbb.service;

import com.cbb.pojo.User;

/** 
 * 类描述:
 * 作者: 地铁与人海
 * 创建日期:2019年3月20日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */

public interface UserService {

	/** 
	 * 方法描述:根据id查询
	 * @param id
	 * @return
	 */
	public User selectById(int id);
	/** 
	 * 方法描述:根据用户输入的条件查找用户
	 * @param map
	 * @return
	 */
	public List<User> selectIf(Map<String, Object> map);
}

开发实现类

package com.cbb.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cbb.mapper.UserMapper;
import com.cbb.pojo.User;
import com.cbb.service.UserService;

/** 
 * 类描述:
 * 作者: 地铁与人海
 * 创建日期:2019年3月20日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User selectById(int id) {

		return userMapper.selectById(id);
	}
	@Override
	public List<User> selectIf(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return userMapper.selectIf(map);
	}
}

写controller类,后台

package com.cbb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cbb.pojo.User;
import com.cbb.service.UserService;

/** 
 * 类描述:
 * 作者: 地铁与人海
 * 创建日期:2019年3月20日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0   
 */
@RequestMapping("User")
@Controller
public class UserController {
	
	@Autowired
	private UserService userService;

	@RequestMapping(value="select")
	public String select(int id ,Model model) {
		User user = userService.selectById(id);
		model.addAttribute("user", user);
		return "/WEB-INF/user.jsp";
		
	}
	
	@RequestMapping(value="select2")
	public String select2(HttpServletRequest request ,HttpServletResponse response,Model model) {
		
		Map<String, Object> map = new HashMap<>();
		map.put("username",request.getParameter("username"));
		map.put("sex", request.getParameter("sex"));
		
		List<User> users = userService.selectIf(map);
		
		model.addAttribute("users", users);
		
		
		return "/index.jsp";
	}
}

写jsp文件,测试根据id查找用户信息

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
</head>
<body>
	
	<table>
		<tr>
			<td>id</td>
			<td>名字</td>
			<td>性别</td>
			<td>生日</td>
			<td>地址</td>
		</tr>
		<tr>
			<td>${user.id }</td>
			<td>${user.username }</td>
			<td>${user.sex }</td>
			<td>${user.birthday }</td>
			<td>${user.address }</td>
		</tr>
	</table>
</body>
</html>

在浏览器输入http://localhost:8080/ssm/user/select?id=2,就可以查看运行效果了

我们再写一个index.jsp页面
用于根据用户输入的数据进行查询,查询结果再返回index.jsp页面

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
</head>
<body>
	<form action="${pageContext.request.contextPath}/user/select2" method="post">
		用户名:<input type="text" name="username"> 
		性别:<select name="sex">
			<option >男</option>
			<option>女</option>
		</select>
		
		<button type="submit">查询</button>
	</form>
	
	<table >
		<tr>
			<td>id</td>
			<td>名字</td>
			<td>性别</td>
			<td>生日</td>
			<td>地址</td>
		</tr>
		<c:forEach items="${users}" var="user">
				<tr class="">
					<td>${user.id }</td>
					<td>${user.username }</td>
					<td>
						${user.sex}
					</td>
					<td>${user.birthday }</td>
					<td>${user.address }</td>
				</tr>
		</c:forEach>
	</table>
</body>
</html>

好了,也可以看效果了,本回合到此结束

END

猜你喜欢

转载自blog.csdn.net/qq_37989076/article/details/88660996