Eclipse编写SSM(增删改查)框架项目

首先创建动态web项目:


接下来写代码:

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<!--过滤器-->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>
        

springmvc-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.1.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.1.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
	default-autowire="byName">

	<!--数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url"
			value="jdbc:mysql://localhost:3306/edoc?useUnicode=true&amp;characterEncoding=utf-8" />
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="username" value="root" />
		<property name="password" value="994994" />
	</bean>
	<!-- 会话工厂 -->
	<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="mapperLocations" value="classpath:mapping/*.xml" />
		<property name="typeAliasesPackage" value="com.accp.entity" />
	</bean>
	<!-- 要注入的事务 -->
	<bean name="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager" />
	<!-- 要切入的事务 -->
	<tx:advice id="txAdvide">
		<tx:attributes>
			<tx:method name="add*" />
			<tx:method name="update*" />
			<tx:method name="delete*" />
			<tx:method name="query*" propagation="NOT_SUPPORTED" />
		</tx:attributes>
	</tx:advice>
	<!-- 指定在哪里切入事务 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.accp.biz.*.*(..))"
			id="pointcut" />
		<aop:advisor advice-ref="txAdvide" pointcut-ref="pointcut" />
	</aop:config>

	<!-- 自动扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.accp.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- 扫描注解包 -->
	<context:component-scan base-package="com.accp" />

	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

实体类代码:

package com.accp.entity;

import java.util.Date;

public class edoc_entry {
	private Integer id; //文档编号
	private Integer categoryId; //分类编号
	private String title; //文档名称
	private String summary; //文档摘要
	private String uploadUser; //上传人
	private Date createDate; //上传时间
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getUploadUser() {
		return uploadUser;
	}
	public void setUploadUser(String uploadUser) {
		this.uploadUser = uploadUser;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
}
package com.accp.entity;

public class edoc_category {
	private Integer id; //分类编号
	private String name; //分类名称
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	

}

Dao层代码:

package com.accp.dao;

import java.util.List;

import com.accp.entity.edoc_category;

public interface edoc_categoryDao {
	List<edoc_category> seeCategoryList(); //查询分类
	
}
package com.accp.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.accp.entity.edoc_entry;

public interface edoc_entryDao {
	List<edoc_entry> seeEntryList(@Param("categoryId")Integer categoryId); //查询列表
	
	int insertEntry(edoc_entry entry); //添加方法
	
	int delEntry(edoc_entry entry); //删除方法
	
	int updEntry(edoc_entry entry); //修改方法
	
	edoc_entry updByIdEntry(edoc_entry entry); //修改页面传值
}

mapping映射文件代码:

<?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.accp.dao.edoc_entryDao">

	<select id="seeEntryList" resultType="edoc_entry">
		select * from edoc_entry
		<where>
			<if test="categoryId>0">
				categoryId = #{categoryId}
			</if>
		</where>
	</select>

	<insert id="insertEntry">
		insert into edoc_entry
		values(default,#{categoryId},#{title},#{summary},#{uploadUser},#{createDate})
	</insert>

	<delete id="delEntry">
		delete from edoc_entry where id = #{id}
	</delete>

	<update id="updEntry">
		update edoc_entry
		<trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
			<if test="title != null">title = #{title},</if>
			<if test="summary != null">summary = #{summary},</if>
			<if test="uploadUser != null">uploadUser = #{uploadUser},</if>
			<if test="createDate != null">createDate = #{createDate},</if>
		</trim>
	</update>

	<select id="updByIdEntry" resultType="edoc_entry">
		select * from edoc_entry
		where id = #{id}
	</select>
</mapper>
<?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.accp.dao.edoc_categoryDao">
    
	<select id="seeCategoryList" resultType="edoc_category">
		select * from edoc_category
	</select>

</mapper>

biz层代码:

package com.accp.biz;

import java.util.List;

import com.accp.entity.edoc_category;

public interface edoc_categoryBiz {
	List<edoc_category> seeCategoryList(); //查询分类
}
package com.accp.biz;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.accp.entity.edoc_entry;

public interface edoc_entryBiz {
List<edoc_entry> seeEntryList(@Param("categoryId")Integer categoryId); //查询列表
	
	int insertEntry(edoc_entry entry); //添加方法
	
	int delEntry(edoc_entry entry); //删除方法
	
	int updEntry(edoc_entry entry); //修改方法
	
	edoc_entry updByIdEntry(edoc_entry entry); //修改页面传值
}

biz.impl实现类代码:

package com.accp.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.accp.biz.edoc_categoryBiz;
import com.accp.dao.edoc_categoryDao;
import com.accp.entity.edoc_category;

@Service
public class edoc_categoryBizImpl implements edoc_categoryBiz {
	
	@Resource
	private edoc_categoryDao categoryDao;

	@Override
	public List<edoc_category> seeCategoryList() {
		// TODO Auto-generated method stub
		return categoryDao.seeCategoryList();
	}

}
package com.accp.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.accp.biz.edoc_entryBiz;
import com.accp.dao.edoc_entryDao;
import com.accp.entity.edoc_entry;

@Service
public class edoc_entryBizImpl implements edoc_entryBiz {
	
	@Resource
	private edoc_entryDao entryDao;

	@Override
	public List<edoc_entry> seeEntryList(Integer categoryId) {
		// TODO Auto-generated method stub
		return entryDao.seeEntryList(categoryId);
	}

	@Override
	public int insertEntry(edoc_entry entry) {
		// TODO Auto-generated method stub
		return entryDao.insertEntry(entry);
	}

	@Override
	public int delEntry(edoc_entry entry) {
		// TODO Auto-generated method stub
		return entryDao.delEntry(entry);
	}

	@Override
	public int updEntry(edoc_entry entry) {
		// TODO Auto-generated method stub
		return entryDao.updEntry(entry);
	}

	@Override
	public edoc_entry updByIdEntry(edoc_entry entry) {
		// TODO Auto-generated method stub
		return entryDao.updByIdEntry(entry);
	}

	
}

control控制层代码:

package com.accp.control;

import javax.annotation.Resource;

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.accp.biz.edoc_categoryBiz;
import com.accp.biz.edoc_entryBiz;
import com.accp.entity.edoc_category;
import com.accp.entity.edoc_entry;

@Controller
public class edoc_entryControl {
	
	@Resource
	private edoc_entryBiz entrybiz;
	@Resource
	private edoc_categoryBiz categoryBiz;
	
	@RequestMapping("/showcategory")
	public String showcategory(@RequestParam(required=false,defaultValue="0")Integer categoryId,Model m) {
		m.addAttribute("catList", categoryBiz.seeCategoryList());
		m.addAttribute("entryList",entrybiz.seeEntryList(categoryId));
		return "index";
	}
	
	@RequestMapping("/addentry")
	public String addentry(edoc_entry e) {
		entrybiz.insertEntry(e);
		return "redirect:/showcategory";
	}
	
	@RequestMapping("/deleteentry")
	public String deleteentry(edoc_entry e) {
		entrybiz.delEntry(e);
		return "redirect:/showcategory";
	}
	
	@RequestMapping("/updateentry")
	public String updateentry(edoc_entry e) {
		entrybiz.updEntry(e);
		return "redirect:/showcategory";
	}
	
	@RequestMapping("/updateByIdentry")
	public String updateByIdentry(edoc_entry e,Model m) {
		m.addAttribute("entryMsg",entrybiz.updByIdEntry(e));
		return "update";
	}
	

}

页面代码:

首页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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="showcategory" method="post" align="center">
		<select name="categoryId">
			<option value="0">--请选择--</option>
			<c:forEach items="${catList}" var="cat">
				<option value="${cat.id}">${cat.name}</option>
			</c:forEach>
		</select>
		<input type="submit" value="查询"/>
	</form>
	<a href="add.jsp">添加</a>
	<table border="1px" width="100%">
		<tr>
			<td>文档编号</td>
			<td>文档名称</td>
			<td>文档摘要</td>
			<td>上传人</td>
			<td>上传时间</td>
			<td colspan="2">操作</td>
		</tr>
		<c:forEach items="${entryList}" var="elist">
			<tr>
				<td>${elist.id}</td>
				<td>${elist.title}</td>
				<td>${elist.summary}</td>
				<td>${elist.uploadUser}</td>
				<td><fmt:formatDate value="${elist.createDate}" pattern="yyyy/MM/dd"/></td>
				<td><a href="updateByIdentry?id=${elist.id}">修改</a> <a
					href="deleteentry?id=${elist.id}">删除</a></td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

添加页面:

<%@ 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="addentry" method="post">
		<table border="1px" align="center" width="30%">
			<input type="hidden" name="categoryId" value="1" />
			<tr>
				<td>文档名称:<input type="text" name="title" /></td>
			</tr>
			<tr>
				<td>文档摘要:<input type="text" name="summary" /></td>
			</tr>
			<tr>
				<td>上传人:<input type="text" name="uploadUser" /></td>
			</tr>
			<tr>
				<td>上传时间:<input type="text" name="createDate" /></td>
			</tr>
			<tr>
				<td>
				<input type="submit" value="提交">
				<input type="button" value="返回" onclick="window.history.go(-1)"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

修改页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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="updateentry" method="post">
		<table border="1px" align="center" width="30%">
		    <input type="hidden" name="id" value="${entryMsg.id}" />
			<input type="hidden" name="categoryId" value="1" />
			<tr>
				<td>文档名称:<input type="text" name="title"
					value="${entryMsg.title}" /></td>
			</tr>
			<tr>
				<td>文档摘要:<input type="text" name="summary"
					value="${entryMsg.summary}" /></td>
			</tr>
			<tr>
				<td>上传人:<input type="text" name="uploadUser"
					value="${entryMsg.uploadUser}" /></td>
			</tr>
			<tr>
				<td>上传时间:<input type="text" name="createDate"
					value="<fmt:formatDate value="${entryMsg.createDate}" pattern="yyyy/MM/dd"/>" /></td>
			</tr>
			<tr>
				<td>
				<input type="submit" value="提交">
				<input type="button" value="返回" onclick="window.history.go(-1)"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

效果显示:

猜你喜欢

转载自blog.csdn.net/weixin_41595700/article/details/86179222