6. mybatis配置
conf/mybatis-config.xml(mybatis配置的基本文件)
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE configuration |
03 | PUBLIC "-//mybatis.org//DTD Config 3.0//EN" |
04 | "http://mybatis.org/dtd/mybatis-3-config.dtd"> |
08 | < typeAlias alias = "User" type = "cn.springmvc.model.User" /> |
mapper/UserMapper.xml(mybatis的实现)
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
04 | "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
05 | < mapper namespace = "cn.springmvc.dao.UserDAO" > |
07 | < insert id = "insertUser" parameterType = "User" keyProperty = "id" > |
08 | insert into days_user( |
这样就完成了基本插入用户的功能!
7. junit测试插入功能
cn.springmvc.test/UserTest.java(用户测试模块)
01 | package cn.springmvc.test; |
03 | import org.junit.Before; |
04 | import org.junit.Test; |
05 | import org.springframework.context.ApplicationContext; |
06 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
08 | import cn.springmvc.model.User; |
09 | import cn.springmvc.service.UserService; |
13 | public class UserTest { |
15 | private UserService userService; |
19 | @SuppressWarnings ( "resource" ) |
20 | ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{ "classpath:conf/spring.xml" |
21 | , "classpath:conf/spring-mybatis.xml" }); |
22 | userService = (UserService) context.getBean( "userServiceImpl" ); |
26 | public void addUser(){ |
27 | User user = new User(); |
28 | user.setNickname( "你好" ); |
30 | System.out.println(userService.insertUser(user)); |
测试结果如图:
![maven+springMVC+mybatis+junit详细搭建过程](https://i-blog.csdnimg.cn/blog_migrate/f8ead03b131955e122393c4907aa8f49.png)
即完成插入功能!
8. springMVC模块搭建
web.xml(web功能配置)
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
04 | xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
05 | id = "WebApp_ID" version = "2.5" > |
06 | < display-name >Archetype Created Web Application</ display-name > |
10 | < param-name >contextConfigLocation</ param-name > |
11 | < param-value >classpath:conf/spring.xml; |
12 | classpath:conf/spring-mybatis.xml |
17 | < param-name >webAppRootKey</ param-name > |
18 | < param-value >springmvc.root</ param-value > |
24 | < filter-name >SpringEncodingFilter</ filter-name > |
25 | < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > |
27 | < param-name >encoding</ param-name > |
28 | < param-value >UTF-8</ param-value > |
31 | < param-name >forceEncoding</ param-name > |
32 | < param-value >true</ param-value > |
36 | < filter-name >SpringEncodingFilter</ filter-name > |
37 | < url-pattern >/*</ url-pattern > |
43 | < param-name >log4jConfigLocation</ param-name > |
44 | < param-value >classpath:conf/log4j.properties</ param-value > |
48 | < param-name >log4jRefreshInterval</ param-name > |
49 | < param-value >6000</ param-value > |
52 | < listener-class >org.springframework.web.util.Log4jConfigListener</ listener-class > |
56 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
61 | < servlet-name >spring</ servlet-name > |
62 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
64 | < param-name >contextConfigLocation</ param-name > |
65 | < param-value >classpath:conf/spring-mvc.xml</ param-value > |
67 | < load-on-startup >2</ load-on-startup > |
70 | < servlet-name >spring</ servlet-name > |
71 | < url-pattern >*.do</ url-pattern > |
75 | < welcome-file >index.jsp</ welcome-file > |
81 | < error-code >404</ error-code > |
82 | < location >/WEB-INF/errorpage/404.jsp</ location > |
86 | < error-code >405</ error-code > |
87 | < location >/WEB-INF/errorpage/405.jsp</ location > |
91 | < error-code >500</ error-code > |
92 | < location >/WEB-INF/errorpage/500.jsp</ location > |
conf/spring-mvc.xml(mvc配置文件)
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "http://www.springframework.org/schema/beans" |
03 | xmlns:p = "http://www.springframework.org/schema/p" |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xmlns:context = "http://www.springframework.org/schema/context" |
06 | xmlns:mvc = "http://www.springframework.org/schema/mvc" |
08 | http://www.springframework.org/schema/beans |
09 | http://www.springframework.org/schema/beans/spring-beans-3.2.xsd |
10 | http://www.springframework.org/schema/context |
11 | http://www.springframework.org/schema/context/spring-context-3.2.xsd |
12 | http://www.springframework.org/schema/mvc |
13 | http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> |
16 | < context:component-scan base-package = "cn.springmvc.controller" /> |
19 | < bean id = "jacksonMessageConverter" class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > |
20 | < property name = "supportedMediaTypes" > |
22 | < value >text/html;charset=UTF-8</ value > |
28 | < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" |
29 | p:prefix = "/WEB-INF/jsp/" p:suffix = ".jsp" /> |