springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

一.说明

1.1 概述说明

1.本案例是通过使用springmvc+spring+jdbcTemplate框架组合实现对account用户的新增和查询功能,其中遇到的坑如下:

1.2 遇到的坑1

1.以下关于视图层,访问的controller的转换映射代码必须放到spring-mvc的配置文件,不能放到spring的配置文件springContextconfig.xml中

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

放到spring的配置文件springContextconfig.xml中,报404错误

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

1.3 遇到的坑2在controller中:

findAll方法中:参数不能写Model model,否则报错

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作 

1.4 遇到的坑3

在save方法中,不能将Account account 这个表单提交的实体参数做个方法的第一个参数,放到第一的位置,否则报404错误

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

二.构建项目

2.1 项目结构图

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.2 新建项目

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

一路傻瓜式,填写,然后创建好的工程,是没有web插件的,resource文件夹也没有,需要自己创建

2.3 配置webapp

2.3.1 工程

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.3.2 module

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.4 配置facets

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.5 配置项目的pom依赖

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>4.2.6.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <mysql.version>5.1.6</mysql.version>
  </properties>

  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <!-- spring mvc-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
  </dependencies>

2.6 配置实体

package com.ljf.spring.jp.sm.demo.bean;

import java.io.Serializable;

/**
 * @ClassName: Account
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/02/19 23:21:27 
 * @Version: V1.0
 **/
public class Account implements Serializable {
    private Integer id;
    private String accountName;
    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", accountName='" + accountName + '\'' +
                ", money=" + money +
                '}';
    }
}

2.7 配置dao层

接口:

package com.ljf.spring.jp.sm.demo.dao;

import com.ljf.spring.jp.sm.demo.bean.Account;

import java.util.List;

public interface AccountDao {
    /**
     * 根据名称查询账户
     * @return
     */
    List<Account> findAll();

    /**
     * 更新账户
     * @param account
     */
    public void saveAccount(Account account);
}

实现层

package com.ljf.spring.jp.sm.demo.dao.impl;

import com.ljf.spring.jp.sm.demo.bean.Account;
import com.ljf.spring.jp.sm.demo.dao.AccountDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

/**
 * @ClassName: AccountDaoImpl
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/02/22 18:02:28 
 * @Version: V1.0
 **/
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Override
    public List<Account> findAll() {
        List<Account> accounts = super.getJdbcTemplate().query("select id,account_name accountName,money from tb_account",new BeanPropertyRowMapper<Account>(Account.class));
         return  accounts;
    }

    @Override
    public void saveAccount(Account account) {
        super.getJdbcTemplate().update("insert into tb_account(id,account_name,money) values(?,?,?)",null,account.getAccountName(),account.getMoney());
    }
}

2.8 service层

1.接口

package com.ljf.spring.jp.sm.demo.service;

import com.ljf.spring.jp.sm.demo.bean.Account;

import java.util.List;

public interface AccountService {
    // 查询所有账户
    public List<Account> findAll() ;

    // 保存帐户信息
    public void saveAccount(Account account);
}

2.实现层

package com.ljf.spring.jp.sm.demo.service.impl;

import com.ljf.spring.jp.sm.demo.bean.Account;
import com.ljf.spring.jp.sm.demo.dao.AccountDao;
import com.ljf.spring.jp.sm.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AccountServiceImpl
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/02/19 23:25:38 
 * @Version: V1.0
 **/

public class AccountServiceImpl implements AccountService {
    //spring的ioc 通过setter方法注入,属性名为actDao和spring-beans.xml中的标签名的id对应
    private AccountDao actDao;
    public AccountDao getActDao() {
        return actDao;
    }

    public void setActDao(AccountDao actDao) {
        this.actDao = actDao;
    }
    @Override
    public List<Account> findAll()  {
        List<Account> accountList=new ArrayList<>();
        accountList =actDao.findAll();
        return accountList;
    }

    @Override
    public void saveAccount(Account account) {
        actDao.saveAccount(account);
    }
}

2.9 controller

package com.ljf.spring.jp.sm.demo.controller;

import com.ljf.spring.jp.sm.demo.bean.Account;
import com.ljf.spring.jp.sm.demo.dao.AccountDao;
import com.ljf.spring.jp.sm.demo.service.AccountService;
import org.apache.log4j.Logger;
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.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;//在webmvc3.0.2包下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: AccountController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/02/20 14:33:00 
 * @Version: V1.0
 **/
//@Controller
//@RequestMapping("/account")
public class AccountController  extends MultiActionController {
    private static Logger log = Logger.getLogger(AccountController.class);
    private AccountService actService;
    public AccountService getActService() {
        return actService;
    }
    public void setActService(AccountService actService) {
        this.actService = actService;
    }

    // @RequestMapping("/findAll")
   public ModelAndView findAll(HttpServletRequest request,HttpServletResponse response,Model model){
       System.out.println("表现层:查询所有账户...");
       log.info("表现层:查询所有账户...");
        List<Account> list = actService.findAll();
       ModelAndView modelAndView=new ModelAndView();
       modelAndView.addObject("list",list);
       modelAndView.setViewName("account_list");
       return modelAndView;
   }

    /**
     * 保存
     * @return
     */
   // @RequestMapping("/save")
    public ModelAndView update(HttpServletRequest request,HttpServletResponse response){
        System.out.println("----update----");
        return new ModelAndView("/multi","method","update");
    }
    /**
     * 保存
     * @return
     */
    // @RequestMapping("/save")
    public void save(HttpServletRequest request,HttpServletResponse response,Account account) throws IOException {
        log.info("表现层:保存账户信息...");
        String id=request.getParameter("id");
        String name=request.getParameter("accountName");
        String money=request.getParameter("money");
        System.out.println("name:"+name+"  "+account.getAccountName());
        actService.saveAccount(account);
        response.sendRedirect(request.getContextPath()+"/account?action=findAll");
        return;
    }
}

2.10 resouces的配置文件

2.10.1 log4j的日志文件


######################
## set log levels
######################
log4j.rootLogger=DEBUG,info,CONSOLE,FILEOUT
#DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE
log4j.addivity.org.apache=true
######################
## CONSOLE Appender ##
######################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c] \:%m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n\u00A0
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
######################
## Rolling File Appender
######################
log4j.appender.FILEOUT=org.apache.log4j.RollingFileAppender
log4j.appender.FILEOUT.File=E:/logs/allError.log
#log4j.appender.FILEOUT.Threshold=ALL
log4j.appender.FILEOUT.Threshold=INFO
log4j.appender.FILEOUT.Append=true
log4j.appender.fileout.MaxFileSize=1000KB
#log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.FILEOUT.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c] \:%m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n\u00A0
#######################
#info log
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.info.File=d:/info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout 
log4j.appender.info.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c] \:%m%n
### File Appender
#######################
#log4j.appender.FILE=org.apache.log4j.FileAppender
#log4j.appender.FILE.File=E:/logs/all.log
#log4j.appender.FILE.Append=true
#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c] \:%m%n
##log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n\u00A0
#####################
## Socket Appender
#####################
#log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
#log4j.appender.SOCKET.RemoteHost=localhost
#log4j.appender.SOCKET.Port=5001
#log4j.appender.SOCKET.LocationInfo=true
## Set up for Log Facter 5
#log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
#log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n
#########################
## Log Factor 5 Appender
#########################
#log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
#log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
########################
## SMTP Appender
########################
#log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
#log4j.appender.MAIL.Threshold=FATAL
#log4j.appender.MAIL.BufferSize=10
#log4j.appender.MAIL.From=openwolfl@163.com
#log4j.appender.MAIL.SMTPHost=mail.openwolf.com
#log4j.appender.MAIL.Subject=Log4J Message
#log4j.appender.MAIL.To=openwolfl@163.com
#log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
#log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#########################
## JDBC Appender
########################
#log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
#log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
#log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
#log4j.appender.DATABASE.user=root
#log4j.appender.DATABASE.password=root
#log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
#log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.A1.File=SampleMessages.log4j
#log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'
#log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
####################
### myself Appender
####################
#log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
#log4j.appender.im.host = mail.cybercorlin.net
#log4j.appender.im.username = username
#log4j.appender.im.password = password
#log4j.appender.im.recipient = corlin@yeqiangwei.com
#log4j.appender.im.layout=org.apache.log4j.PatternLayout
#log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

2.10.2 jdbc的信息文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/nongda
jdbc.username=root
jdbc.password=

2.10.3 springmvc的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">



    <!--step1:开启注解扫描,只扫描Controller注解-->
    <context:component-scan base-package="com.ljf.spring.jp.sm.demo">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!--step2:配置的视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--step3:过滤静态资源-->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**" />

    <!--step4:开启SpringMVC注解的支持-->
    <mvc:annotation-driven/>
    <!-- Controller方法调用规则定义
  配置MultiActionController使用的方法对应策略ParameterMehtodNameResolver,
  用于解析请求中的特定参数的值,将该值作为方法名调用 -->
    <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName"><value>action</value></property>
    </bean>
    <!-- step6.配置controller层-->
    <bean name="/account" class="com.ljf.spring.jp.sm.demo.controller.AccountController">
        <property name="actService" ref="acService"></property>
        <property name="methodNameResolver" ref="paramMethodResolver"></property>
    </bean>
    <!-- servlet映射列表,所有控制层Controller的servlet在这里定义 -->

    <bean name="/test1/multi" class="com.ljf.spring.jp.sm.demo.controller.MultiController">
        <property name="methodNameResolver">
            <ref bean="paramMethodResolver"/>
        </property>
    </bean>
    <bean name="/test1/helloworld" class="com.ljf.spring.jp.sm.demo.controller.HelloWorldController" />

</beans>

2.10.4 spring的配置文件

<?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: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">
    <!--step1:开启注解的扫描,希望处理service和dao,但是controller不需要Spring框架去处理-->
    <context:component-scan base-package="com.ljf.spring.jp.sm.demo" >
        <!--排除controller的扫描,配置哪些注解不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!--step2:加载jdbc连接信息 -->
    <context:property-placeholder location="classpath:spring-jdbc.properties"></context:property-placeholder>
    <!--step3:配置连接池-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- step4.配置账户的持久层-->
    <bean id="accountDao" class="com.ljf.spring.jp.sm.demo.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- step5.配置业务层-->
    <bean id="acService" class="com.ljf.spring.jp.sm.demo.service.impl.AccountServiceImpl">
        <property name="actDao" ref="accountDao"></property>
    </bean>

    <!--step6:配置Spring框架声明式事务管理-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>

    <!--配置AOP 织入增强-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ljf.spring.jp.sm.demo.service.impl.*ServiceImpl.*(..))"/>
    </aop:config>
</beans>

2.10.5 web.xml的内容配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <display-name>Archetype Created Web Application</display-name>

    <!--step1:配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
    <!--设置配置文件的路径 这里是放到了resouces目录下-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--   需要多个加载,可以多起几个context-param标签
    <context-param>
        <param-name/>
        <param-value/>
    </context-param>
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--step2:配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加载spring-mvc.xml配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--启动服务器,创建该servlet-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--step3:解决中文乱码的过滤器-->
    <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>
</web-app>

2.11 前端页面

1.index页面

<%--
  Created by IntelliJ IDEA.
  User: jurfl
  Date: 2021/2/20
  Time: 14:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/account?action=findAll">测试查询</a>

<h3>账户录入页面</h3>

<form action="${pageContext.request.contextPath}/account?action=save" method="post">
    姓名:<input type="text" name="accountName" /><br/>
    金额:<input type="text" name="money" /><br/>
    <input type="submit" value="保存"/><br/>
</form>

</body>
</body>
</html>

2.list页面

<%--
  Created by IntelliJ IDEA.
  User: jurfl
  Date: 2021/2/20
  Time: 14:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>查询所有的帐户</h3>
<table border="1" cellpadding="1" cellspacing="1" width="500px" >
    <tr>
        <td>id</td>
        <td>姓名</td>
        <td>金额</td>
    </tr>
<c:forEach items="${list}" var="account">
    <tr>
        <td>${account.id}</td>
        <td>${account.accountName}</td>
        <td>${account.money}</td>
    </tr>
</c:forEach>
</table>

</body>
</html>

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.12 静态资源目录的创建

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.13 生产war包

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.14 部署tomcat

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

2.14 启动服务,访问

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

springmvc+jdbcTemplate+spring基于xml的方式实现数据crud操作

上一篇:Mybatis学习过程记录


下一篇:java之log4j日志处理