使用springmvc+mybatis实现增删改查并进行登录注册功能
2017-01-20 08:50
1036 查看
一、第一次使用springmvc+mybatis整合出的框架,使用的是maven版本
1.使用maven有以下主要要创建maven文件夹,导入maven包并进行配置环境变量,在后面的文章中会提到maven配置。使用maven主要是依赖其pom.xml配置文件进行导入jar包的操作,浏览器打开:mvnrepository.com搜索包就可以了,在<dependency>标签中加入搜索出的链接会自动进行下载。
2.在这里使用的是mybatis代码生成器,可以生成出mapper、mapper.xml以及model层,具体使用配置在generator.xml中
配置好自己的表名,然后再右键pom.xml,选择run as maven build path,输入mybatis-generator:generate从而实现代码生成功能,其他mybatis.xml以及spring和springmvc配置文件会在之后的源代码中,可以进行参考。
3.之后就要自己创建controller层来进行控制层的功能
(1)在controller层遇到的问题及注意事项
a.要设置@controller注解,告诉springmvc这是控制类层,才可以读取到该层
b.使用了@Autowired来进行自动注入了mapper层,具体@Autowired和@Resource区别可参考其他博客
c.对于@RequestMapping的使用
我这次只使用了几种方式,如@RequestMapping("方法名")、@RequestMapping(value="/方法名",method=RequestMethod.POST)后面加方法的这种,不过method方法在从前台数据通过表单post方法传递过来时method写post可以,但从后台controller传递到前台时写post会报错,不知道什么原因,改为了get方法.而且当使用了后面跟method这种方法时,String类型的方法不能返回到页面只能返回到controller方法中
4.Controller层方法
a. list方法,进行查询出所有的数据返回到列表中
需要注意的,参数为ModelAndView进行参数的传递,t1为前面通过自动注入得到的mapper类,ModelAndView可以通过m.addObject来进行对list的传递,类似以前servlet中的setAttribute封装,当使用ModelAndView时可以通过其方法m.setViewName(“”)来进行页面的跳转括号内为跳转的页面名称,最后return返回的值为m;
在跳转的ui.jsp采用的是easyui布局结合css,其中接受值通过EL表达式
b.编辑方法edit,前台点击名字传递一个a标签的href指向一个edit方法,跳转到了controller中的edit方法,同时把该条信息的id传递过来
在一个form表单中得到通过对象.出来的信息,然后跳转到controller层中的save方法进行修改操作保存
c.添加操作add1,在前面ui.jsp页面有链接指向添加操作,为add1
这时跳转到了add.jsp页面中
提交的form action为add方法
执行了insert方法,增加的为一个对象,并返回到list方法中
d.删除操作del方法
e.登录注册功能
由于数据库表是之前的,所以字段中没有password,暂时使用性别来进行当做密码来进行识别。
设置一个主的登录界面跳转到login.jsp页面进行登录
这时传递过来的参数是前台传的一个实体,然后通过实体.get得到需要的字段值,对应mapper中有一个返回实体的方法
之后的注册功能实际上是又调用了一次增加方法。
部分功能如跳转问题,拦截器问题,非空校验还没有完善,之后会进行补充。
数据库图
1.使用maven有以下主要要创建maven文件夹,导入maven包并进行配置环境变量,在后面的文章中会提到maven配置。使用maven主要是依赖其pom.xml配置文件进行导入jar包的操作,浏览器打开:mvnrepository.com搜索包就可以了,在<dependency>标签中加入搜索出的链接会自动进行下载。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>wb.cn</groupId> <artifactId>springDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springDemo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.3.RELEASE</spring.version> <junit.version>4.12</junit.version> <aspectj.version>1.8.9</aspectj.version> <jackson.json.version>2.7.4</jackson.json.version> <fasterxml.jackson.version>2.7.4</fasterxml.jackson.version> <codehaus.woodstox.version>4.4.1</codehaus.woodstox.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- servlet/ jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</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-oxm</artifactId> <version>${spring.version}</version> </dependency> <!-- jackson json --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.json.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.json.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.json.version}</version> </dependency> <!-- jackson xml --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>${fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>woodstox-core-asl</artifactId> <version>${codehaus.woodstox.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- 日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils --> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.6</version> </dependency> </dependencies> <build> <finalName>sprintmvc-mybaitis</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</ 4000 groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <configurationFile>src/main/resources/generator.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
2.在这里使用的是mybatis代码生成器,可以生成出mapper、mapper.xml以及model层,具体使用配置在generator.xml中
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite --> <!-- 数据库驱动包位置 --> <classPathEntry location="D:\maven\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar" /> <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />--> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/bpmx33" userId="root" password="123456"> <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oraclF:thin:@localhost:1521:orcl" userId="msa" password="msa">--> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在me.gacl.domain这个包下 --> <javaModelGenerator targetPackage="lyb.cn.ssm.model" targetProject="C:\Users\Administrator\Desktop\springDeom\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在me.gacl.mapping这个包下 --> <sqlMapGenerator targetPackage="lyb.cn.ssm.mapper" targetProject="C:\Users\Administrator\Desktop\springDeom\src\main\java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在me.gacl.dao这个包下 --> <javaClientGenerator type="XMLMAPPER" targetPackage="lyb.cn.ssm.dao" targetProject="C:\Users\Administrator\Desktop\springDeom\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="w_test" domainObjectName="Test" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
配置好自己的表名,然后再右键pom.xml,选择run as maven build path,输入mybatis-generator:generate从而实现代码生成功能,其他mybatis.xml以及spring和springmvc配置文件会在之后的源代码中,可以进行参考。
3.之后就要自己创建controller层来进行控制层的功能
(1)在controller层遇到的问题及注意事项
a.要设置@controller注解,告诉springmvc这是控制类层,才可以读取到该层
b.使用了@Autowired来进行自动注入了mapper层,具体@Autowired和@Resource区别可参考其他博客
c.对于@RequestMapping的使用
我这次只使用了几种方式,如@RequestMapping("方法名")、@RequestMapping(value="/方法名",method=RequestMethod.POST)后面加方法的这种,不过method方法在从前台数据通过表单post方法传递过来时method写post可以,但从后台controller传递到前台时写post会报错,不知道什么原因,改为了get方法.而且当使用了后面跟method这种方法时,String类型的方法不能返回到页面只能返回到controller方法中
4.Controller层方法
a. list方法,进行查询出所有的数据返回到列表中
@RequestMapping("list") public ModelAndView testList(ModelAndView m){ //System.out.println(t1.selectByPrimaryKey(10000000160001L)); List<Test> list = new ArrayList<Test>(); list = t1.selectList(); m.addObject("list", list); m.addObject("test", "123"); for(int i = 0;i<list.size();i++){ System.out.println(list.get(i).getfXm()); } m.setViewName("ui"); return m; }
需要注意的,参数为ModelAndView进行参数的传递,t1为前面通过自动注入得到的mapper类,ModelAndView可以通过m.addObject来进行对list的传递,类似以前servlet中的setAttribute封装,当使用ModelAndView时可以通过其方法m.setViewName(“”)来进行页面的跳转括号内为跳转的页面名称,最后return返回的值为m;
在跳转的ui.jsp采用的是easyui布局结合css,其中接受值通过EL表达式
<div id="content" region="center" title="数据汇总" style="padding: 20px; "> <div> <ul> <a href="add1"><input type = "button" value = "添加"></a> <a href="index"><input type = "button" value = "返回登录"></a> </ul> <table class="tablelist" style="width: 800px; height: 400px;" data-options="rownumbers:true,singleSelect:true,selectOnCheck:false,checkOnSelect:false,pagination:true,pageSize:20,url:'',method:'get',striped:true,rowStyler:setRowBgColor"> <thead> <tr> <th>选择</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>爱好</th> <th>日期</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${list}" var="Bean"> <tr class="odd"> <td><input type="radio" name="Id" value="${Bean.id }"></td> <td><a href="edit?id=${Bean.id}" dir="ltr" title="点击查看详情" onclick="">${Bean.fXm }</a></td> <td>${Bean.fXb }</td> <td>${Bean.fNl }</td> <td>${Bean.fDxk }</td> <%-- <td>${Bean.fRq }</td> --%> <td><fmt:formatDate value="${Bean.fRq}" pattern="yyyy-MM-dd" /> </td> <td><a href="del?id=${Bean.id}" dir="ltr" onclick="">删除</a></td> </tr> </c:forEach> <c:if test="${empty list}"> <tr class="odd"> <td colspan="5" align="center">没有查询到相关记录!</td> </tr> </c:if> </tbody> </table> </div> </div>
b.编辑方法edit,前台点击名字传递一个a标签的href指向一个edit方法,跳转到了controller中的edit方法,同时把该条信息的id传递过来
@RequestMapping(value="/edit",method=RequestMethod.GET) public ModelAndView edit(Long id,ModelAndView m,HttpServletRequest request) throws Exception { System.out.println("进入edit方法"); //String fXm = request.getParameter("fXm"); Test t = t1.selectByPrimaryKey(id); m.setViewName("edit"); m.addObject("t", t); System.out.println(t); return m; }对应的mapper是通过id查找信息的dao方法,返回一个对象,并跳转到edit页面中
<div class="row-fluid"> <div class="span12"> <form action="save" method="post"> <fieldset> <legend>id</legend> <input type="text" name="id" value="${t.id} " /> <legend>名称</legend> <input type="text" name="fXm" value="${t.fXm}"/> <legend>性别</legend> <input type="text" name="fXb" value="${t.fXb }"/> <legend>年龄</legend> <input type="text" name="fNl" value="${t.fNl }"/> <legend>日期</legend> <input type="text" name="fRq" value="<fmt:formatDate value="${t.fRq}" pattern="yyyy-MM-dd" />"/> <legend>爱好</legend> <input type="text" name="fDxk" value="${t.fDxk }"/> <button type="submit" class="btn">提交</button> </fieldset> </form> </div> </div>
在一个form表单中得到通过对象.出来的信息,然后跳转到controller层中的save方法进行修改操作保存
@RequestMapping(value="/save",method=RequestMethod.POST) public String save(ModelAndView m,Test t){ t1.updateByPrimaryKeySelective(t); //t1.updateByPrimaryKey(t); System.out.println("执行了更新方法"); // t1.insert(t); //t1.updateByPrimaryKey(t); //m.setViewName("list"); //m.addObject("", attributeValue) return "redirect:list"; }这里是进行了一个通过对象来执行更新操作。之后return的方法redirect意思是跳转到controller中的方法list,又进行一次查询列表的操作。
c.添加操作add1,在前面ui.jsp页面有链接指向添加操作,为add1
@RequestMapping(value="/add1",method=RequestMethod.GET) public ModelAndView add1(ModelAndView m,Test t){ System.out.println("进入了add1方法"); m.setViewName("add"); m.addObject(t); return m; }
这时跳转到了add.jsp页面中
<form action="add" method="post"> <fieldset> <legend>id</legend> <input type="text" name="id" /> <legend>名称</legend> <input type="text" name="fXm" /> <legend>性别</legend> <input type="text" name="fXb" /> <legend>年龄</legend> <input type="text" name="fNl" /> <legend>爱好</legend> <input type="text" name="fDxk" /> <legend>日期</legend> <input type="text" name="fRq" value="<fmt:formatDate value="${t.fRq}" pattern="yyyy-MM-dd" />"/> <button type="submit" class="btn">提交</button> </fieldset> </form>
提交的form action为add方法
@RequestMapping(value="/add",method=RequestMethod.POST) public String add(ModelAndView m,Test t){ t1.insert(t); //日期格式的添加问题 return "redirect:list"; }
执行了insert方法,增加的为一个对象,并返回到list方法中
d.删除操作del方法
@RequestMapping("del")//删除操作 public String del(ModelAndView m,Test t){ t1.deleteByPrimaryKey(t.getId()); return "redirect:list"; }问题存在有开了一个页面
e.登录注册功能
由于数据库表是之前的,所以字段中没有password,暂时使用性别来进行当做密码来进行识别。
@RequestMapping("index")// public String index(ModelAndView m,Test t){ //t1.deleteByPrimaryKey(t.getId()); return "login"; }
设置一个主的登录界面跳转到login.jsp页面进行登录
<form action="login" method="post"> <fieldset> <a href="add1"><input type = "button" value = "注册"></a> <h3>请输入您的名字,性别进行验证(测试性别相当于密码)</h3> <legend>名称</legend> <input type="text" name="fXm" /> <legend>性别</legend> <input type="text" name="fXb" /> <button type="submit" class="btn">提交</button> </fieldset> </form>点击提交跳转到controller中的login方法
@RequestMapping("login") public String login(Test t){ String fXm = t.getfXm(); String fXb = t.getfXb(); Test test = t1.selectName(fXm); System.out.println(test+"mapper"); System.out.println(fXb+"jsp"); if(test!=null){ if(fXb.equals(test.getfXb())){ return "redirect:list"; } else{ return "error"; } }else{ return "error"; } }
这时传递过来的参数是前台传的一个实体,然后通过实体.get得到需要的字段值,对应mapper中有一个返回实体的方法
<select id="selectName" resultMap="BaseResultMap"> select * from w_test where F_xm=#{fXm,jdbcType=VARCHAR} </select>通过前台的fXm在数据库中找到其相应的性别(相当于密码),返回一个数据的实体test,为了防止空指针异常,进行if非空判断,然后判断前台的性别和数据库查出来的性别test.getfXb()是否一致来决定返回的路径
之后的注册功能实际上是又调用了一次增加方法。
部分功能如跳转问题,拦截器问题,非空校验还没有完善,之后会进行补充。
数据库图
相关文章推荐
- ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)
- Spring MVC+mybatis实现注册登录功能
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- springmvc+spring+mybatis实现用户登录功能(下)
- springmvc+mybatis+sql server实现简单登录功能【转】
- 使用SpringMVC+Angularjs实现登录功能
- springmvc+mybatis+sql server实现简单登录功能【转】
- 完整使用SSM框架(Spring SpringMVC Mybatis)搭建web服务器实现登录功能
- SpringMVC + Mybatis 实现用户登录功能
- springmvc+spring+mybatis实现用户登录功能(上)
- Spring MVC + mybatis实现的注册登录
- spring+springmvc+mybatis+mysql实现登录功能(下)
- springmvc+mybatis+sql server实现简单登录功能【转】
- Spring+SpringMVC+MyBatis实现数据库连接的登录功能
- springmvc+mybatis+sql server实现简单登录功能【转】