【无标题】

springmvc+mybatis实现增删查改功能
配置文件有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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>activiti-ssm</display-name>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>


    <!-- 配置前端控制器 -->
    <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:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 配置中文编码的过滤器 -->
    <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>

    <!-- 配置加载spring容器的监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 全局参数: xml文件的路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.web.filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/web/*</url-pattern>
    </filter-mapping>

</web-app>

mybatis的配置SqlMapConfig.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>

	
	<!-- 分页插件: 拦截器 -->
    <plugins>
    	<plugin interceptor="com.github.pagehelper.PageHelper">
    		<!-- 指定数据库的方言(类型)必须  -->
    		<property name="dialect" value="mysql"/>
    		<property name="reasonable" value="true"/>
    	</plugin>
    </plugins>
   
	
</configuration>

spring 的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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="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/aop 
		                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
	<!-- 配置外部数据库连接信息-->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="${jdbc.url}" />
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- 配置SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
	</bean>
	
	<!-- 配置mapper的扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.web.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
	<!-- 扫描service包,托管 业务类-->
	<context:component-scan base-package="com.web.service.Impl"/>
	
	<!-- 
			配置事务
	 -->
	<!-- 1.配置事务管理器 -->
	<bean id="transManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 2.配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transManager">
		<tx:attributes>
			<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
			<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
			<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
			<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>

		</tx:attributes>
	</tx:advice>
	
	<!-- 3.配置切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.web.service.*.*(..))" id="aopPointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="aopPointcut" />
	</aop:config>

</beans>                    

springmvc.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-3.2.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/util  
            http://www.springframework.org/schema/util/spring-util-3.2.xsd
            http://www.springframework.org/schema/aop 
		    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    

    
	<mvc:annotation-driven />
	<!-- 
		 加了类型转换器,静态资源 使用此种方法解析
    	 它的意思就是没有映射到的URL交给默认的web容器中的servlet进行处理:
     -->
	<mvc:default-servlet-handler/>



	<!-- 扫描controller包 -->
	<context:component-scan base-package="com.web.controller"></context:component-scan>
	
	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>



</beans>

连接数据库文件
db.properties

jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai&useUnicode=true&nullCatalogMeansCurrent=true&characterEncoding=UTF-8
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=

log4j.properties

log4j.rootLogger=INFO, CA

# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n

针对配置的操作。
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<% pageContext.setAttribute("path", request.getContextPath()); %>
<!DOCTYPE HTML>
<html>
<head>
    <title>首页</title>
    <style type="text/css">
        h1 {
            margin: 20%;
            text-align: center;
            background: black;
            border-radius: 4px;
        }
 
        a {
            color: chartreuse;
            text-decoration: none;
        }
 
        .header {
            width: 700px;
            margin: 0px auto;
            border: solid 1px red;
            position: relative;
        }
 
        .written {
            display: block;
            width: 100%;
            margin: 0;
        }
 
        .img {
            width: 100%;
        }
    </style>
</head>
<body>
<div class="header">
    <img class="img" src="tidalSeaSpirit.jpg">
    <h1 class="written">
        <a href="${path }/user/homePage">大家好!我只是一只绿色Hello World...请对我温柔点,轻轻点击我!</a>
    </h1>
</div>
</body>
</html>

addPage.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>新增</title>
    <style type="text/css">
        .header {
            margin: 10% 0 0 20%;
        }
    </style>
</head>
<body>
<div class="header">
    <h1>新增用户</h1>
    <form action="" name="userForm">
        名字:<input type="text" name="name">
        性别:<input type="text" name="gender">
        称号:<input type="text" name="position">
        <input type="button" value="添加" onclick="addUser()">
    </form>
    <script type="text/javascript">
        function addUser() {
            var form = document.forms[0];
            form.action = "<%=basePath %>user/addUser";
            form.method = "post";
            form.submit();
        }
    </script>
</div>

homePage.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<% String path = request.getContextPath(); %>
<html>
<head>
    <title>列表</title>
    <style type="text/css">
        .header {
            margin: 10% 0 0 20%;
        }
        table{
            border: black solid 1px;
            width: 50%;
            height: 40%;
        }
        .add{
            color: blue;
            text-decoration: none;
            font-size: 20px;
        }
    </style>
</head>
<body>
<div class="header">
    <h1><small>用户列表</small></h1>
    <a class="add" href="${path}/user/addPage">新增</a>
    <table class="table">
        <thead>
            <tr>
                <th>编号</th>
                <th>名字</th>
                <th>性别</th>
                <th>称号</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="user" items="${requestScope.get('list')}" varStatus="status">
                <tr class="content">
                    <td>${user.id}</td>
                    <td>${user.name}</td>
                    <td>${user.gender}</td>
                    <td>${user.position}</td>
                    <td>
                        <a href="${path}/user/updatePage?id=${user.id}">编辑</a> |
                        <a href="<%=path%>/user/delUser/${user.id}">删除</a>
                    </td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</div>
</div>
</body>

UserService

package com.service;
 
import com.pojo.User;
 
import java.util.List;
 
public interface UserService {
    int addUser(User user);
 
    int delUserById(long id);
 
    int updateUser(User user);
 
    User queryById(long id);
 
    List<User> findAllUser();
}
 

以上是一部分代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉莫的羔羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值