博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC、mybatis实现的登录页面(maven)
阅读量:5082 次
发布时间:2019-06-13

本文共 18210 字,大约阅读时间需要 60 分钟。

首先项目结构

pom文件

4.0.0
com.maven
springMvc_MyBatis
war
0.0.1-SNAPSHOT
springMvc_MyBatis Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework
spring-context
3.2.6.RELEASE
org.springframework
spring-jdbc
3.2.6.RELEASE
cglib
cglib
2.2
mysql
mysql-connector-java
5.0.5
dom4j
dom4j
1.6.1
commons-dbcp
commons-dbcp
1.4
commons-logging
commons-logging
1.1.1
commons-collections
commons-collections
3.2.1
antlr
antlr
2.7.7
org.mybatis
mybatis
3.2.3
org.springframework
spring-webmvc
3.1.1.RELEASE
org.springframework
spring-test
3.1.1.RELEASE
org.mybatis
mybatis-spring
1.2.1
c3p0
c3p0
0.9.1.2
org.aspectj
aspectjweaver
1.7.4
org.codehaus.jackson
jackson-core-asl
1.9.11
org.codehaus.jackson
jackson-core-lgpl
1.9.11
org.codehaus.jackson
jackson-mapper-asl
1.9.11
org.codehaus.jackson
jackson-mapper-lgpl
1.9.11
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.2.2
org.apache.httpcomponents
httpclient
4.2.2
log4j
log4j
1.2.9
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
javaee
javaee-api
5
provided
springMvc_MyBatis

 

使用maven install下载需要的jar包。

 

之后一步一步实现这一个简单的小例子。

 

 

一个特别好的例子的网址

http://www.baikeyang.com/code/96610.html

本机运行结果:

 

 

主要代码:

Add.java

package com.bky.model;public class Add {    private String id;    private String tname;    private String tpwd;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id == null ? null : id.trim();    }    public String getTname() {        return tname;    }    public void setTname(String tname) {        this.tname = tname == null ? null : tname.trim();    }    public String getTpwd() {        return tpwd;    }    public void setTpwd(String tpwd) {        this.tpwd = tpwd == null ? null : tpwd.trim();    }}

 

AddMapper.java

package com.bky.dao;import java.util.List;import com.bky.model.Add;public interface AddMapper {    int deleteByPrimaryKey(String id);    int insert(Add record);    int insertSelective(Add record);    Add selectByPrimaryKey(String id);    int updateByPrimaryKeySelective(Add record);    int updateByPrimaryKey(Add record);        List
getAll();}

AddMapper.xml

id, tname, tpwd
delete from tadd where id = #{id,jdbcType=VARCHAR}
insert into tadd (id, tname, tpwd ) values (#{id,jdbcType=VARCHAR}, #{tname,jdbcType=VARCHAR}, #{tpwd,jdbcType=VARCHAR} )
insert into tadd
id,
tname,
tpwd,
#{id,jdbcType=VARCHAR},
#{tname,jdbcType=VARCHAR},
#{tpwd,jdbcType=VARCHAR},
update tadd
tname = #{tname,jdbcType=VARCHAR},
tpwd = #{tpwd,jdbcType=VARCHAR},
where id = #{id,jdbcType=VARCHAR}
update tadd set tname = #{tname,jdbcType=VARCHAR}, tpwd = #{tpwd,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR}

BaseService.java

package com.bky.service;import java.util.List;import com.bky.model.Add;public interface BaseService {        String addInfo(Add addInfo);        List
getAll(); String delete(String id); Add findById(String id); String update(Add addInfo);}

BaseServiceImpl.java

package com.bky.service.impl;import java.util.List;import java.util.UUID;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.bky.dao.AddMapper;import com.bky.model.Add;import com.bky.service.BaseService;@Service("baseService")public class BaseServiceImpl implements BaseService {        private AddMapper addMapper;    public AddMapper getAddMapper() {        return addMapper;    }    @Autowired    public void setAddMapper(AddMapper addMapper) {        this.addMapper = addMapper;    }    @Override    public String addInfo(Add addInfo) {        if (addMapper.insertSelective(addInfo) == 1) {            return "添加成功";        }        return "添加失败";    }    @Override    public List
getAll() { return addMapper.getAll(); } @Override public String delete(String id) { if (addMapper.deleteByPrimaryKey(id) == 1) { return "删除成功"; } return "删除失败"; } @Override public Add findById(String id) { return addMapper.selectByPrimaryKey(id); } @Override public String update(Add addInfo) { if (addMapper.updateByPrimaryKeySelective(addInfo) == 1) { return "更新成功"; } return "更新失败"; } }

BaseController.java

package com.bky.controller;import java.util.List;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.bky.model.Add;import com.bky.service.BaseService;@Controllerpublic class BaseController {        private BaseService baseService;        public BaseService getBaseService() {        return baseService;    }    @Autowired    public void setBaseService(BaseService baseService) {        this.baseService = baseService;    }        @SuppressWarnings("finally")    @RequestMapping("addInfo")    public String add(Add add,HttpServletRequest request){        try {                        add.setId(UUID.randomUUID().toString());            System.out.println(add.getId() + ":::::" + add.getTname() + ":::::" + add.getTpwd());            String str = baseService.addInfo(add);            System.out.println(str);            request.setAttribute("InfoMessage", str);        } catch (Exception e) {            e.printStackTrace();            request.setAttribute("InfoMessage", "添加信息失败!具体异常信息:" + e.getMessage());        } finally {                        return "result";        }    }        @RequestMapping("getAll")    public String getAddInfoAll(HttpServletRequest request){        try {                        List
list = baseService.getAll(); System.out.println(list); request.setAttribute("addLists", list); return "listAll"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "信息载入失败!具体异常信息:" + e.getMessage()); return "result"; } } @SuppressWarnings("finally") @RequestMapping("del") public String del(String tid,HttpServletRequest request){ try { String str = baseService.delete(tid); request.setAttribute("InfoMessage", str); } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "删除信息失败!具体异常信息:" + e.getMessage()); } finally { return "result"; } } @RequestMapping("modify") public String modify(String tid,HttpServletRequest request){ try { Add add = baseService.findById(tid); request.setAttribute("add", add); return "modify"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "信息载入失败!具体异常信息:" + e.getMessage()); return "result"; } } @SuppressWarnings("finally") @RequestMapping("update") public String update(Add add,HttpServletRequest request){ try { String str = baseService.update(add); request.setAttribute("InfoMessage", str); } catch (Exception e) { e.printStackTrace(); request.setAttribute("InfoMessage", "更新信息失败!具体异常信息:" + e.getMessage()); } finally { return "result"; } }}

配置文件

config.properties

#hibernate.dialect=com.bky.util.Oracle10gDialect#driverClassName=oracle.jdbc.driver.OracleDriver#validationQuery=SELECT 1 FROM DUAL#jdbc_url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Baikeyang)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))#jdbc_username=sshe#jdbc_password=1234#hibernate.dialect=org.hibernate.dialect.Oracle9Dialect#driverClassName=oracle.jdbc.driver.OracleDriver#validationQuery=SELECT 1 FROM DUAL#jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl#jdbc_username=sshe#jdbc_password=1234#hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect#driverClassName=com.mysql.jdbc.DrivervalidationQuery=SELECT 1jdbc_url=jdbc:mysql://localhost:3306/bkytest?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNulljdbc_username=rootjdbc_password=root#hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect#driverClassName=net.sourceforge.jtds.jdbc.Driver#validationQuery=SELECT 1#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/dbName#jdbc_username=sa#jdbc_password=123456#jndiName=java:comp/env/BenDiShuJuYuan#hibernate.hbm2ddl.auto=update#hibernate.show_sql=true#hibernate.format_sql=true#hibernate.use_sql_comments=true#sessionInfoName=sessionInfo#uploadPath=/ssheUploadFile

spring-mvc.xml

spring-mybatis.xml

com.bky.service.*

spring.xml

web.xml

contextConfigLocation
classpath:spring.xml;classpath:spring-mybatis.xml
字符集过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
字符集编码
encoding
UTF-8
encodingFilter
/*
spring监听器
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
spring mvc servlet
springMvc
org.springframework.web.servlet.DispatcherServlet
spring mvc 配置文件
contextConfigLocation
classpath:spring-mvc.xml
1
springMvc
*.do
15
index.jsp

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
新增数据 查看全部数据

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              添加数据        
用户名:
密码:

 

 listall.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              所有数据        
所有结果
编号 用户名 密码 操作
${list.id} ${list.tname} ${list.tpwd} 更新    删除

 

转载于:https://www.cnblogs.com/yukino/p/4022605.html

你可能感兴趣的文章
LUOGU P3178 [HAOI2015]树上操作
查看>>
XML文件中怎么写小于号 等特殊符号
查看>>
flask-session
查看>>
Global Cache CR Requested But Current Block Received
查看>>
(转)C# Enum,Int,String的互相转换 枚举转换--非常实用
查看>>
HNU 11722 The Gougu Theorem
查看>>
大一下第2次作业(markdown改)
查看>>
宿舍桶装水配送系统参考
查看>>
计数排序
查看>>
取硬币
查看>>
然之协同系统6.4.1 SQL注入导致getshell
查看>>
本地wampserver如何配置伪静态
查看>>
python中的*和**的用途
查看>>
细说unittest-2
查看>>
windows+nginx+fcgi配置
查看>>
Logistic Regression and Newton's Method
查看>>
第八章:Junit——在 Eclipse 中里面 导入 Junit 的 jar 包
查看>>
关于MySQL5.7 几天的总结(简单分区 & json类型)
查看>>
Buns(dp+多重背包)
查看>>
拦截form表单的提交
查看>>