javaEE期末准备

JAVA SSM

结构图

在这里插入图片描述

编写配置文件

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test(你的数据库名)
jdbc.username=root
jdbc.password=root

log4j.properties

# Global logging configuration
log4j.rootLogger=debug,ERROR,stdout
# MyBatis logging configuration...
log4j.logger.com.itheima=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

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>
    <!--别名定义-->
	<typeAliases>
        <!--name为你实体对象的包-->
		<package name="cn.gary.pojo"/>
	</typeAliases>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<property name="helperDialect" value="mysql"/>
			<property name="reasonable" value="true"/>
		</plugin>
	</plugins>

</configuration>

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:aop="http://www.springframework.org/schema/aop"
	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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!-- 数据源配置 -->    
<context:property-placeholder location="classpath:db.properties"/>
<bean id = "dataSource" class= "org.apache.commons.dbcp2.BasicDataSource">
	<property name="driverClassName" value = "${jdbc.driver}" />
	<property name="url" value = "${jdbc.url}" />
	<property name="username" value ="${jdbc.username}" />
	<property name="password" value = "${jdbc.password}" />
</bean>
    
<!--配置事务管理器 开启事务注解-->
<bean id = "transctionManager" 
	class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref = "dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transctionManager" />

  <!--配置mybatis工厂-->
<bean id = "sqlSessionFactory"
			class = "org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref = "dataSource" />
			<property name="configLocation" value = "classpath:mybatisConfig.xml" />
</bean>
	<!--配置mapper扫描器-->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!--value值为你数据库操作的包-->
       <property name="basePackage" value="cn.gary.dao"/>
   </bean>
  <context:component-scan base-package="cn.gary.service" />


</beans>

springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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-4.3.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<context:component-scan
		base-package="com.itheima.controller" />
	<mvc:annotation-driven />
	<!--视图解析器,描述前缀和后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>  

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>exam</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文件监听器,就是配置你数据源的那个文件-->
	<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>
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encoding</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>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置mvc前端控制器 ,就是你配置视图解析器那个文件 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

主体

pojo包

在该包下创建你所需的对应实体类

dao包(接口和mapper)

增删改查样例

先创建一个接口(最好与接下来的Mapper同名)
StuMapper.java

package cn.gary.dao;

import java.util.List;

import cn.gary.pojo.Stu;

public interface StuMapper {
	public Stu getStuBySno(int sno);
	public List<Stu> getAllStu();
	public List<Stu> getBysname(String sname) ;
	public int addStu(Stu stu);
	public int deleStu(int sno);
	public int updateStu(Stu stu);
}

StuMapper.xml(注意接口中方法与xml中id名相同)

<?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="cn.gary.dao.StuMapper">
 <!-- 查询单个 -->
 	<select id="getStuBySno"  parameterType="Integer" resultType="cn.gary.pojo.Stu">
 		select * from stu_tab where sno=#{_parameter}
 	</select>
 	<!-- 查询所有 -->
 	<select id = "getAllStu" resultType="cn.gary.pojo.Stu" >
 		select * from stu_tab
 	</select>
 	<!-- 模糊查询 -->
 	<select id = "getBysname" parameterType="String"  resultType="cn.gary.pojo.Stu" >
 		select * from stu_tab where sname like concat('%',#{_parameter},'%')
 	</select> 	
 	<!-- 添加 -->
 	<insert id="addStu" parameterType="cn.gary.pojo.Stu">
 		insert into
 		stu_tab(sname,sclass,age) values(#{sname},#{sclass},#{age})
 	</insert>
 	<!-- 删除 -->
 	<delete id="deleStu" parameterType="Integer">
 		delete from  stu_tab where sid = #{_parameter}
 	</delete>
 	<!-- 更新 -->
 	<update id="updateStu" parameterType="cn.gary.pojo.Stu">
 		update stu_tab
 		set sname = #{sname},
 		sclass = #{sclass},
 		age = #{age},
 		where sid = #{sid}
 	</update>
 </mapper>

service包

同样先写一个对应的接口
Stuservice.java

package cn.gary.service;

import java.util.List;

import cn.gary.pojo.Stu;

public interface Stuservice {
	public Stu getStuBySno(int sno);
	public List<Stu> getAllStu();
	public List<Stu> getBysname(String sname) ;
	public int addStu(Stu stu);
	public int deleStu(int sno);
	public int updateStu(Stu stu);
}

serviceimpl.java(在这里对应实现)(没写全hh)

package cn.gary.service;

import java.util.List;

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

import cn.gary.dao.StuMapper;
import cn.gary.pojo.Stu;

@Service
public class StuserviceImpl implements Stuservice {
	@Autowired
	StuMapper stuMapper;
	@Override
	public Stu getStuBySno(int sno) {
		return stuMapper.getStuBySno(sno);
	}
	@Override
	public List<Stu> getAllStu() {
		return stuMapper.getAllStu();
	}
}

controller包(分页使用与其jsp页面)

其中更新操作流程 :先在页面取得sid(主键),通过controller传到修改页面,最后再执行更新

package cn.gary.controller;

import java.util.List;

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 org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gary.pojo.Stu;
import cn.gary.service.Stuservice;

@Controller
@RequestMapping("/stu")
public class StuController {
	
	@Autowired
	Stuservice stuservice;
	
	@RequestMapping("/getStu")
	public String getStu (int sno, Model model) {
		Stu stu = stuservice.getStuBySno(sno);
		model.addAttribute("stu",stu);
		return "stuinfo";
	}
	@RequestMapping("/getAll")
	public String getAll(Model model,@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "3")int pageSize) {
		PageHelper.startPage(pageNum,pageSize);//页码和页大小
		List<Stu> uList = stuservice.getAllStu();
		PageInfo<Stu> p = new PageInfo<Stu>(uList);
		
		model.addAttribute("ulist",uList);
		model.addAttribute("pageinfo",p);
		
		return "stuAll";
	}
		//添加
	@RequestMapping("/toaddstu")
	public String toAdd() {
		return "addstuP";
	}
	@RequestMapping("/addStu")
	public String addStu(Stu stu) {
		int t = stuservice.addStu(stu);
		if(t>0) {
			return "forward:getAll";
		}
		else 
			return "erro";
	}
	//更新
	@RequestMapping("/toupstu")
	public String toupdate(int sno,Model model) {
		model.addAttribute("sno",sno);
		return "upstuP";
	}
	@RequestMapping("/upStu")
	public String upStu(Stu stu) {
		int t = stuservice.updateStu(stu);
		if(t>0) {
			return "forward:getAll";
		}
		else 
			return "erro";
	}
	//删除
	public String deleStu(int sno) {
		int t = stuservice.deleStu(sno);
		if(t>0) {
			return "forward:getAll";
		}
		else 
			return "erro";
	}
}

jsp页面

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method = "post" action = "${pageContext.request.contextPath}/stu/getstu">
		<input type = "text" name = "sname"/>
		<input type = "submit" value = "提交"/>	
	</form>		<a href = "#">添加</a>
	
	<table border="1px" width = "500px">
		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>年级</th>
			<th>年龄</th>
		</tr>
		<c:forEach items="${ulist}" var = "x">
			<tr>
				<th>${x.sno}</th>
				<th>${x.sname}</th>
				<th>${x.sclass}</th>
				<th>${x.age}</th>
			</tr>
		</c:forEach>
	</table>
	第${pageinfo.pageNum}页/共${pageinfo.pages}页
	<a href="${pageContext.request.contextPath}/stu/getAll?pageNum=${pageinfo.prePage}">上一页</a>
		<c:forEach begin="1" end = "${pageinfo.pages}" step = "1" var = "y">
			<a href="${pageContext.request.contextPath}/stu/getAll?pageNum=${y}">${y}</a>
		</c:forEach>
	
	
	<a href="${pageContext.request.contextPath}/stu/getAll?pageNum=${pageinfo.nextPage}">下一页</a>
</body>
</html>
posted @ 2021-12-22 21:43  我才是Gary  阅读(24)  评论(0)    收藏  举报