基于SSM框架进行简单的增删改查

1、项目目录和页面

2、配置文件

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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


	<context:annotation-config />
	<context:component-scan base-package="com.ssm.service" />

	<!-- 数据库基本配置  -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- 基本属性 url、user、password -->
		<property name="url" value="jdbc:mysql://localhost:3306/school?characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="1234" />
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="3" />
		<property name="minIdle" value="3" />
		<property name="maxActive" value="20" />
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="60000" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 1" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
	</bean>



	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="typeAliasesPackage" value="com.ssm.entity" />
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml" />
		<!-- 分页PageHelper插件配置 -->
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<!--使用下面的方式配置参数,一行配置一个 -->
						<value></value>
					</property>
				</bean>
			</array>
		</property>
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ssm.mapper" />
	</bean>



</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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">



	<context:annotation-config />

	<context:component-scan base-package="com.ssm.controller">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<mvc:annotation-driven />

	<mvc:default-servlet-handler />

	<!-- 视图定位 页面存放路径-->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<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://java.sun.com/xml/ns/javaee" xmlns:web="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_2_5.xsd"
	version="2.5">

	<!-- 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>

	<!-- 设置post编码格式 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- spring mvc核心:分发servlet -->
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- spring mvc的配置文件 -->
		<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>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>



</web-app>

3、逻辑代码

package com.ssm.entity;

/***
 * 用户表实体
 * @author lvjun
 *
 */
public class UserEntity {
	
	private int id;
	private String uname;
	private int sex;//0女 1男 3保密
	private int age;
	private String card;
	private String addr;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getCard() {
		return card;
	}
	public void setCard(String card) {
		this.card = card;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	@Override
	public String toString() {
		return "UserEntity [id=" + id + ", uname=" + uname + ", sex=" + sex + ", age=" + age + ", card=" + card
				+ ", addr=" + addr + "]";
	}
	
	
	
	

}
package com.ssm.mapper;

import java.util.List;

import com.ssm.entity.UserEntity;

/***
 * 
 * @author lvjun
 *
 */
public interface UserMapper {
	
	public List<UserEntity> listM();//查询所有
	
	public UserEntity getM(int id); // 查询单条
	
	public int addM(UserEntity userEntity); // 添加
	
	public void deleteM(int id); // 删除
	
	public int editM(UserEntity userEntity); // 修改

}
<?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.ssm.mapper.UserMapper">

	
	<select id="listM" resultType="UserEntity">
		select * from users 
	</select>

	<select id="getM" parameterType="_int" resultType="UserEntity">
		select * from users where id= #{id}
	</select>

    <delete id="deleteM" parameterType="UserEntity">
		delete from users where id= #{id}
	</delete>
	
	<update id="editM" parameterType="UserEntity">
		update users set uname=#{uname},sex=#{sex},age=#{age},card=#{card},addr=#{addr}   where id= #{id}
	</update>

    <insert id="addM" parameterType="UserEntity">
		INSERT INTO users(uname,sex,age,card,addr)VALUES(#{uname},#{sex},#{age},#{card},#{addr})
	</insert>



</mapper>

package com.ssm.service;

import java.util.List;

import com.ssm.entity.UserEntity;

/***
 * 
 * @author lvjun
 *
 */
public interface UserService {

	 List<UserEntity> list();//查询所有
	
	 UserEntity get(int id); // 查询单条
	
	 int add(UserEntity userEntity); // 添加
	
	 void delete(int id); // 删除
	
	 int edit(UserEntity userEntity); // 修改
}
package com.ssm.service.impl;

import java.util.List;

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

import com.ssm.entity.UserEntity;
import com.ssm.mapper.UserMapper;
import com.ssm.service.UserService;

/***
 * 
 * @author lvjun
 *
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserMapper userMapper;

	@Override
	public List<UserEntity> list() {
		// TODO Auto-generated method stub
		return userMapper.listM();
	}

	@Override
	public UserEntity get(int id) {
		// TODO Auto-generated method stub
		return userMapper.getM(id);
	}

	@Override
	public int add(UserEntity userEntity) {
		// TODO Auto-generated method stub
		return userMapper.addM(userEntity);
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		userMapper.deleteM(id);
	}

	@Override
	public int edit(UserEntity userEntity) {
		// TODO Auto-generated method stub
		return userMapper.editM(userEntity);
	}

	
}
package com.ssm.controller;

import java.util.List;

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.ssm.entity.UserEntity;
import com.ssm.service.UserService;

/***
 * 
 * 
 * @author lvjun
 *
 */
@Controller
@RequestMapping("")
public class UserController {

	@Autowired
	UserService userService;

	// 返回
	@RequestMapping("fanhui")
	public ModelAndView fanhui() {
		ModelAndView mav = new ModelAndView("redirect:/list");
		return mav;
	}

	// 查询列表
	@RequestMapping("list")
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		List<UserEntity> ls = userService.list();
		mav.addObject("ls", ls);
		mav.setViewName("index");
		return mav;
	}

	// 查询明细
	@RequestMapping("get")
	public ModelAndView get(int id) {
		UserEntity c = userService.get(id);
		ModelAndView mav = new ModelAndView("detail");
		mav.addObject("c", c);
		return mav;
	}

	// 单条明细修改跳转到修改页面
	@RequestMapping("editgo")
	public ModelAndView editgo(int id) {
		UserEntity c = userService.get(id);
		ModelAndView mav = new ModelAndView("edit");
		mav.addObject("c", c);
		return mav;
	}

	// 修改
	@RequestMapping("edit")
	public ModelAndView edit(UserEntity userEntity) {
		userService.edit(userEntity);
		ModelAndView mav = new ModelAndView("redirect:/list");
		return mav;
	}

	// 删除
	@RequestMapping("delete")
	public ModelAndView delete(int id) {
		userService.delete(id);
		ModelAndView mav = new ModelAndView("redirect:/list");
		return mav;
	}

	// 添加   跳转 到添加页面
	@RequestMapping("addgo")
	public ModelAndView addgo() {
		ModelAndView mav = new ModelAndView("add");
		return mav;
	}

	// 添加数据
	@RequestMapping("add")
	public ModelAndView add(UserEntity userEntity) {
		userService.add(userEntity);
		ModelAndView mav = new ModelAndView("redirect:/list");
		return mav;
	}

}

4、页面代码

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
				
		<table border="1">
			
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>性别</th>
				<th>年龄</th>
				<th>身份证</th>
				<th>籍贯</th>
				<th colspan="3">操作</th>
			</tr>
			
			<c:forEach items="${ls}" var="d" varStatus="st">
				<tr>
					<td>${d.id}</td>
					<td>${d.uname}</td>
					<td>${d.sex}</td>
					<td>${d.age}</td>
					<td>${d.card}</td>
					<td>${d.addr}</td>
					<td>
						<a href="get?id=${d.id}">详情</a>
					</td>
					<td>
						<a href="editgo?id=${d.id}">修改</a>
					</td>
					<td>
						<a href="delete?id=${d.id}">删除</a>
					</td>
				</tr>
			</c:forEach>			
			
			<tr>
				<td colspan="9">
					<a href="addgo">添加</a>
				</td>
			</tr>
			
		</table>
			
	</body>
</html>
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
	<head>
		<meta charset="UTF-8">
		<title>修改页面</title>
	</head>
	<body>
	
	<form method="post" action="edit">	
		<table border="1">
			
			<tr>
				<td colspan="2">修改用户信息</td>
			</tr>
			<tr>
				<td>编号</td>
				<td>
					<input type="text" name="id" value="${c.id}" readonly/>
				</td>
			</tr>
			<tr>
				<td>姓名</td>
				<td>
					<input type="text" name="uname" value="${c.uname }" />
				</td>
			</tr>
			<tr>
				<td>性别</td>
				<td>
					<input type="text" name="sex" value="${c.sex }"/>
				</td>
			</tr>
			<tr>
				<td>年龄</td>
				<td>
					<input type="text" name="age" value="${c.age }" />
				</td>
			</tr>
			<tr>
				<td>身份证</td>
				<td>
					<input type="text" name="card" value="${c.card }"/>
				</td>
			</tr>
			<tr>
				<td>地址</td>
				<td>
					<input type="text" name="addr" value="${c.addr }"/>
				</td>
			</tr>

			<tr>
				<td>
					<a href="/ssmcrud/fanhui">返回</a>
				</td>
				<td>
					<input type="submit" value="提交" />					
				</td>
			</tr>
			
			
		</table>
		
		</form>
		
	</body>
</html>
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta charset="UTF-8">
<title>详情页面</title>
</head>
<body>

	<table border="1">

		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
			<th>身份证</th>
			<th>籍贯</th>
		</tr>
		<tr>
			<td>${c.id}</td>
			<td>${c.uname}</td>
			<td>${c.sex}</td>
			<td>${c.age}</td>
			<td>${c.card}</td>
			<td>${c.addr}</td>
		</tr>

		<tr>
			<td colspan="6"><a href="/ssmcrud/fanhui">返回</a></td>
		</tr>

	</table>


</body>
</html>

 

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
	<head>
		<meta charset="UTF-8">
		<title>添加</title>
	</head>
	<body>
	
	<form method="post" action="add">	
		
		<table border="1">
			<tr>
				<td colspan="2">添加用户信息</td>
			</tr>
			<tr>
				<td>姓名</td>
				<td>
					<input type="text" name="uname" />
				</td>
			</tr>
			<tr>
				<td>性别</td>
				<td>
					<input type="text" name="sex" />
				</td>
			</tr>
			<tr>
				<td>年龄</td>
				<td>
					<input type="text" name="age" />
				</td>
			</tr>
			<tr>
				<td>身份证</td>
				<td>
					<input type="text" name="card" />
				</td>
			</tr>
			<tr>
				<td>地址</td>
				<td>
					<input type="text" name="addr" />
				</td>
			</tr>

			<tr>
				<td>
					<a href="/ssmcrud/fanhui">返回</a>
				</td>
				<td>
					<input type="submit" value="提交" />					
				</td>
			</tr>
			
			
		</table>
		
		</form>
	</body>
</html>

 

 

没有更多推荐了,返回首页