06_MyBatis,Spring,SpringMVC整合
项目结构
Spring的配置:
beans.xml
<?xml <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 <context:component-scan <bean <property <property <property <property </bean> <!-- 使用spring来管理sqlSessionFactory --> <bean <property <property </bean> <bean <property </bean> <tx:advice <tx:attributes> <tx:method <tx:method <tx:method <tx:method </tx:attributes> </tx:advice> <aop:config> <aop:advisor </aop:config> </beans> |
springmvc.xml的配置内容如下:
<?xml <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 <context:component-scan <bean <property <property </bean> </beans> |
web.xml的配置内容如下:
<?xml <web-app 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" <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>SpringCharacterEncodingFilter</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>SpringCharacterEncodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> </web-app> |
5
log4j.properties
log4j.rootLogger=DEBUG, #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG |
sqlMapConfig.xml
<?xml <!DOCTYPE PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias </typeAliases> <mappers> <mapper </mappers> </configuration> |
PersonMapper.xml
<?xml <!DOCTYPE PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:命名空间,用来唯一标识一个映射文件,命名规范就是当前的文件的包加上文件名 --> <mapper <resultMap <!-- column:库表的字段 property:库表所有映射的实体类中属性名 --> <id <result <result <result <result </resultMap> <!-- 公用的sql片段,也可以接收参数动态sql,所有的sql可以 --> <sql PERSON_ID, NAME, GENDER, PERSON_ADDR, BIRTHDAY </sql> <!-- 根据id来查询一个Person的数据 sql语句接收参数的一个语法#{},如果接收的是一个{}中的内容任意select id:sql语句的唯一的标识不能重复 parameterType:sql要接收的数据类型 resultType:sql所返回的数据类型 --> <!-- 实际项目中数据库的表的字段一般由多个单词来构成 在java的model的实体类中的属性多个单词的命名规范是驼峰模式personAddr --> <!-- useCache:控制当前的这个sql是否使用二级缓存 --> <select select * from person t where t.person_id = #{id} </select> <!-- <insert <selectKey select LAST_INSERT_ID() </selectKey> insert into person (person_id, name, gender, person_addr, birthday) values(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday}) </insert> <!-- 删除的sql不能使用别名 --> <delete delete from person where person_id = #{personId} </delete> <!-- <!-- map.put("name", "安"); map.put("gender", "0"); map.put("personAddr", "东京") map.put("birthday", new Date()); <where>会自动处理and, --> <select select * from person t <where> <if t.name like '%${name}%' </if> <if and t.gender = #{gender} </if> <if and t.person_addr like '%${personAddr}%' </if> <if <![CDATA[ and t.birthday < #{birthday} ]]> </if> </where> </select> <!-- 动态修改 <set>标签可以去掉最后一个逗号 flushCache:二级缓存的刷新的配置:默认是true:会刷新,如果false就不刷新缓存 --> <update update person t <set> <if t.name = #{name}, </if> <if t.gender = #{gender}, </if> <if t.person_addr = #{personAddr}, </if> <if t.birthday = #{birthday} </if> </set> where t.person_id = #{personId} </update> </mapper> |
6
Person.java的内容如下:
package com.rl.model; import java.io.Serializable; import java.util.Date; public class Person implements Serializable{ private static final long serialVersionUID = 7728191033619971201L; private Integer personId; private String name; private String gender; private String personAddr; private Date birthday; public Integer getPersonId() { return personId; } public void setPersonId(Integer personId) { this.personId = personId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPersonAddr() { return personAddr; } public void setPersonAddr(String personAddr) { this.personAddr = personAddr; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } } |
7
PersonDao.java
package com.rl.dao; import java.util.List; import java.util.Map; import com.rl.model.Person; public interface PersonDao { public void save(Person person); public Person selectPersonById(Integer personId); public void update(Person person); public void delete(Integer personId); public List<Person> selectPersonByCondition(Map<String, Object> map); } |
8
PersonDaoImpl.java的内容如下:
package com.rl.dao.impl; import java.util.List; import java.util.Map; import org.mybatis.spring.support.SqlSessionDaoSupport; import org.springframework.stereotype.Repository; import com.rl.dao.PersonDao; import com.rl.model.Person; @Repository public class PersonDaoImpl extends SqlSessionDaoSupport implements PersonDao { //命名空间 String ns = "com.rl.mapper.PersonMapper."; @Override public void save(Person person) { this.getSqlSession().insert(ns+"insert", person); } @Override public Person selectPersonById(Integer personId) { return (Person) this.getSqlSession().selectOne(ns+"selectPersonById", personId); } @Override public void update(Person person) { this.getSqlSession().update(ns+"dynamicUpdate", person); } @Override public void delete(Integer personId) { this.getSqlSession().delete(ns+"delete", personId); } @SuppressWarnings("unchecked") @Override public List<Person> selectPersonByCondition(Map<String, Object> map) { return this.getSqlSession().selectList(ns+"selectPersonByCondition", map); } } |
9
PersonService.java
package com.rl.service; import java.util.List; import java.util.Map; import com.rl.model.Person; public interface PersonService { public void save(Person person); public Person selectPersonById(Integer personId); public void update(Person person); public void delete(Integer personId); public List<Person> selectPersonByCondition(Map<String, Object> map); } |
10
PersonServiceImpl.java
package com.rl.service.impl; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.rl.dao.PersonDao; import com.rl.model.Person; import com.rl.service.PersonService; @Service public class PersonServiceImpl implements PersonService { @Autowired private PersonDao personDao; @Override public void save(Person person) { personDao.save(person); } @Override public Person selectPersonById(Integer personId) { return personDao.selectPersonById(personId); } @Override public void update(Person person) { personDao.update(person); } @Override public void delete(Integer personId) { personDao.delete(personId); } @Override public List<Person> selectPersonByCondition(Map<String, Object> map) { return personDao.selectPersonByCondition(map); } } |
11
PersonController.java的内容如下:
package com.rl.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import com.rl.model.Person; import com.rl.service.PersonService; @Controller @RequestMapping("/person") public class PersonController { @Autowired private PersonService personService; /** * 跳转到添加页面 */ @RequestMapping("/toSave.do") public String toSave(){ return "add"; } /** * 添加Person */ @RequestMapping("/save.do") public String save(Person person){ personService.save(person); return "redirect:listPerson.do"; } /** * 查询Person */ @RequestMapping("/getPerson.do") public String getPerson(Integer personId, Model model){ Person person = personService.selectPersonById(personId); model.addAttribute("person", person); return "update"; } /** * 修改Person */ @RequestMapping("/update.do") public String update(Person person){ personService.update(person); return "redirect:listPerson.do"; } /** * 删除 */ @RequestMapping("/delete.do") public String delete(Integer personId){ personService.delete(personId); return "redirect:listPerson.do"; } /** * 动态条件组合查询 */ @RequestMapping("/listPerson.do") public String listPerson(String name, String gender, String personAddr, Date birthday, Model model){ Map<String, Object> map = new HashMap<String,Object>(); if(name != null && "".equals(name.trim())){ name = null; } if(gender != null && "".equals(gender.trim())){ gender = null; } if(personAddr != null && "".equals(personAddr.trim())){ personAddr = null; } map.put("name", name); map.put("gender", gender); map.put("personAddr", personAddr); map.put("birthday", birthday); List<Person> pList = personService.selectPersonByCondition(map); model.addAttribute("pList", pList); model.addAttribute("name", name); model.addAttribute("gender", gender); model.addAttribute("personAddr", personAddr); model.addAttribute("birthday", birthday); return "list"; } @InitBinder public void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } } |
12
add.jsp
<%@ <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib <%@taglib <!DOCTYPE <html> <head> <base <title>My <meta <meta <meta <meta <meta <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form <table <tr> <td><label>姓名:</label><input <td><label>性别:</label> <select <option <option <option </select> </td> <td><label>地址:</label><input <td><label>生日:</label><input <td><input </tr> </table> </form> </body> </html> |
13 list.jsp
<%@ <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib <%@taglib <!DOCTYPE <html> <head> <base <title>My <meta <meta <meta <meta <meta <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script if(confirm("真删吗?")){ window.location.href="person/delete.do?personId="+personId; } } </script> </head> <body> <form <table <tr> <td><label>姓名:</label><input <td><label>性别:</label> <select <option <option <option </select> </td> <td><label>地址:</label><input <td><label>生日:</label><input <td><input </tr> </table> </form> <table <tr> <td><a </tr> </table> <table <tr> <th>姓名</th> <th>性别</th> <th>地址</th> <th>生日</th> <th>操作</th> </tr> <c:forEach <tr> <td>${person.name <td> <c:if <c:if </td> <td>${person.personAddr <td><f:formatDate <td><a <a </tr> </c:forEach> </table> </body> </html> |
14 update.jsp
<%@ <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib <%@taglib <!DOCTYPE <html> <head> <base <title>My <meta <meta <meta <meta <meta <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form <input <table <tr> <td><label>姓名:</label><input <td><label>性别:</label> <select <option <option <option </select> </td> <td><label>地址:</label><input <td><label>生日:</label><input <td><input </tr> </table> </form> </body> </html> |
06_MyBatis,Spring,SpringMVC整合的更多相关文章
- spring+springMVC 整合 MongoDB 实现注册登录
发现一入手 MongoDB,便无法脱离,简要说一下,MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 也是在 Nosql 中我最喜欢的一种 ...
- Spring+SpringMVC整合----配置文件
1.在 web.xml 中加载 spring 的配置文件 bean.xml 底层是 Listener <!-- Spring --> <context-param> &l ...
- Spring Boot2 系列教程(二十四)Spring Boot 整合 Jpa
Spring Boot 中的数据持久化方案前面给大伙介绍了两种了,一个是 JdbcTemplate,还有一个 MyBatis,JdbcTemplate 配置简单,使用也简单,但是功能也非常有限,MyB ...
- Eclipse Meaven Spring SpringMVC Mybaits整合
本示例是在:Ubuntu15上实现的:Windows上安装Maven将不太相同. Maven Install Run command sudo apt-get install maven, to in ...
- Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- Spring+SpringMVC+Mybatis大整合(SpringMVC采用REST风格、mybatis采用Mapper代理)
整体目录结构: 其中包下全部是采用mybatis自动生成工具生成. mybatis自动生成文件 <?xml version="1.0" encoding="UTF- ...
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- myBatis,Spring,SpringMVC三大框架ssm整合模板
整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration> <!-- 批量别名的设置 -- ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
随机推荐
- ●BZOJ 3676 [Apio2014]回文串
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题解: 后缀数组,Manacher,二分 首先有一个结论:一个串的本质不同的回文串的个 ...
- VK Cup 2017 - Квалификация 2
因为资格赛1已经通过了,资格赛2随便打打玩.这次题目比上次还简单,FallDream看了两眼觉得太水就不做了,我一个人闲着无聊只好默默做了 A. Новый пароль 题目大意:给出N和K,要求构 ...
- 【以2-SAT为主题的婚礼UVA11294】
·新娘头饰复杂,这个婚礼怪异非凡. ·英文题,述大意: 婚宴上,有一个很长的桌子.桌子两边坐人(即人们坐成两排).新娘坐在其中一排,只能看见桌子对面所有的人.输入的m表示有m对人打过架. ...
- PHP Laravel框架入门心得 | How to study PHP Laravel Framework
PHP有不少开发框架,其中比较出名的有Symfony和Laravel. 我说说我最近入门Laravel的感受和学习方法吧. 1.第一个感受是Laravel的社区讨论和学习资源真的是太棒了,中文化也做得 ...
- Atom 编辑器安装 linter-eslint 插件,并配置使其支持 vue 文件中的 js 格式校验
安装方式有如下几种. 1.最常用的安装方式. # 进入atom插件文件夹 cd ~/.atom/packages/ # git clone 插件源文件 git clone https://github ...
- Mysql--存储引擎(MyISam & InnoDB)
Mysql 系列文章主页 =============== 查看 Mysql 支持的存储引擎: show engines; 查看当前数据库使用的存储引擎: show variables like '%s ...
- Go实现海量日志收集系统(四)
到这一步,我的收集系统就已经完成很大一部分工作,我们重新看一下我们之前画的图: 我们已经完成前面的部分,剩下是要完成后半部分,将kafka中的数据扔到ElasticSearch,并且最终通过kiban ...
- Vue-起步篇:Vue与React、 Angular的区别
毋庸置疑,Vue.React. Angular这三个是现在比较火的前端框架.这几个框架都各有所长,选择学习哪种就得看个人喜好或者实际项目了.相比之下, Vue 是轻量级且容易学习掌握的. 1.Vue和 ...
- nginx 网络模型,cpu亲和等优点
nginx优点1.IO多路复用epollIO多路复用:多个描述符的I/O操作都能在一个线程内并发交替地顺序完成,这里的"复用" 指的是复用同一个线程epollIO多路复用的实现方式 ...
- spark升级后 集成hbase-1.0.0-cdh5.4.5异常
.具体场景如下: spark1.6 升级 spark2.2 后 分析查询hbase 数据报异常: 具体错误如下: ERROR TableInputFormat: java.io ...