Maven下Spring-Mybatis注解式整合

本文在maven环境下整合Spring-Mybatis,其中Spring支持当下比较流行的注解注入方式,简化配置

 本文存在问题,不支持数据库事务回滚,数据库存入汉字为乱码。

一、准备工作

eclipse 4.2

maven 3.1.0 

maven安装就不介绍了,eclipse的maven插件版本为3.0.4,本地版本为3.1.0。用哪个版本的差别不大

 

二、创建Maven项目

(1)选择安装maven项目。搜索关键字maven,如下图:


 

(2)这里默认就行了,直接下一步


 

(3)搜索关键字webapp,选择maven-archetype-webapp点击下一步


 

(4)填写工程的Group Id,Artifact Id。

Group Id就是大项目的id,Arifact Id就是该项目的Id,点击完成。如下图:


 

(5)首先,完善目录,增加重要的source Folder,这个不是简单的Floder,这些文件夹是会参与编译的。增加src/main/java目录。如果创建目录时提示目录已经存在,请参考http://yudey.iteye.com/blog/1985181如下图:




(6)在webapp下创建文件夹jsp,在WEB-INF下创建文件夹config

(7)文件目录大体如下:



 (8)将工程变成web工程

此时,我们的工程还不是标准的web工程,可以在eclipse中增加web工程的特性,选择工程的Properties,选Project Facets,点击Convert to faceted form...  如下图:



 
 

这里,我们选择Dynamic Web Module,版本选择2.4,这个版本比较通用,我本机为默认版本。 如下图:



 
这样,我们的工程就完全是一个web工程了。

三、文件配置

(1)配置pom.xml,让jar包飞一会。如果pom.xml保存后,有错误提示,很可能是jar包重复,可注释掉重复的引用

文件pom.xml

 

Xml代码   收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>test</groupId>  
  5.   <artifactId>spring_test</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>spring_test Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <properties>  
  11.         <org.springframework.version>3.0.6.RELEASE</org.springframework.version>  
  12.     </properties>  
  13.     <dependencies>  
  14.         <!-- Spring jar -->  
  15.         <!-- Core utilities used by other modules. Define this if you use Spring   
  16.             Utility APIs (org.springframework.core.*/org.springframework.util.*) -->  
  17.         <dependency>  
  18.             <groupId>org.springframework</groupId>  
  19.             <artifactId>spring-core</artifactId>  
  20.             <version>${org.springframework.version}</version>  
  21.         </dependency>  
  22.         <!-- Expression Language (depends on spring-core) Define this if you use   
  23.             Spring Expression APIs (org.springframework.expression.*) -->  
  24.         <dependency>  
  25.             <groupId>org.springframework</groupId>  
  26.             <artifactId>spring-core</artifactId>  
  27.             <version>${org.springframework.version}</version>  
  28.         </dependency>  
  29.         <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define   
  30.                  this if you use Spring Bean APIs (org.springframework.beans.*)  -->  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-beans</artifactId>  
  34.             <version>${org.springframework.version}</version>  
  35.         </dependency>  
  36.         <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core,   
  37.                  spring-beans) Define this if you use Spring AOP APIs -->  
  38.         <dependency>  
  39.             <groupId>org.springframework</groupId>  
  40.             <artifactId>spring-aop</artifactId>  
  41.             <version>${org.springframework.version}</version>  
  42.         </dependency>  
  43.         <!-- Application Context (depends on spring-core, spring-expression, spring-   
  44.             spring-beans) This is the central artifact for Spring's Dependency Injec   
  45.             Container and is generally always defined -->  
  46.         <!-- <dependency>  
  47.             <groupId>org.springframework</groupId>  
  48.             <artifactId>spring-context</artifactId>  
  49.             <version>3.2.4.RELEASE</version>  
  50.             <type>pom.tmp.sha1.tmp</type>  
  51.         </dependency> -->  
  52.         <!-- Various Application Context utilities, including EhCache, JavaMail,   
  53.                  Quartz, and Freemarker integration Define this if you need any of these -->  
  54.         <dependency>  
  55.             <groupId>org.springframework</groupId>  
  56.             <artifactId>spring-context-support</artifactId>  
  57.             <version>${org.springframework.version}</version>  
  58.         </dependency>  
  59.         <!-- Transaction Management Abstraction (depends on spring-core, spring-bean   
  60.             spring-aop, spring-context) Define this if you use Spring Transactions o   
  61.             DAO Exception Hierarchy (org.springframework.transaction.*) -->  
  62.         <dependency>  
  63.             <groupId>org.springframework</groupId>  
  64.             <artifactId>spring-tx</artifactId>  
  65.             <version>${org.springframework.version}</version>  
  66.         </dependency>  
  67.         <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-   
  68.             spring-tx) Define this if you use Spring's JdbcTemplate API -->  
  69.         <!-- <dependency>  
  70.             <groupId>org.springframework</groupId>  
  71.             <artifactId>spring-jdbc</artifactId>  
  72.             <version>${org.springframework.version}</version>  
  73.         </dependency> -->  
  74.         <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,   
  75.                  and iBatis. (depends on spring-core, spring-beans, spring-context, sprin   
  76.                  Define this if you need ORM (org.springframework.orm.*)  -->  
  77.         <dependency>  
  78.             <groupId>org.springframework</groupId>  
  79.             <artifactId>spring-orm</artifactId>  
  80.             <version>${org.springframework.version}</version>  
  81.         </dependency>  
  82.         <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB,   
  83.                  JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-be   
  84.                      spring-context) Define this if you need OXM -->  
  85.         <dependency>  
  86.             <groupId>org.springframework</groupId>  
  87.             <artifactId>spring-oxm</artifactId>  
  88.             <version>${org.springframework.version}</version>  
  89.         </dependency>  
  90.         <!-- Web application development utilities applicable to both Servlet and   
  91.             Portlet Environments (depends on spring-core, spring-beans, spring-conte   
  92.             Define this if you use Spring MVC, or wish to use Struts, JSF, or anothe   
  93.             web framework with Spring (org.springframework.web.*) -->  
  94.         <dependency>  
  95.             <groupId>org.springframework</groupId>  
  96.             <artifactId>spring-web</artifactId>  
  97.             <version>${org.springframework.version}</version>  
  98.         </dependency>  
  99.         <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-bea   
  100.                  spring-context, spring-web) Define this if you use Spring MVC with a Ser   
  101.                  Container such as Apache Tomcat -->  
  102.         <dependency>  
  103.             <groupId>org.springframework</groupId>  
  104.             <artifactId>spring-webmvc</artifactId>  
  105.             <version>${org.springframework.version}</version>  
  106.         </dependency>  
  107.         <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-bea   
  108.             spring-context, spring-web) Define this if you use Spring MVC with a Por   
  109.             Container (org.springframework.web.portlet.*) -->  
  110.         <dependency>  
  111.             <groupId>org.springframework</groupId>  
  112.             <artifactId>spring-webmvc-portlet</artifactId>  
  113.             <version>${org.springframework.version}</version>  
  114.         </dependency>  
  115.         <!-- Support for testing Spring applications with tools such as JUnit and   
  116.                  TestNG This artifact is generally always defined with a 'test' scope for   
  117.                  the integration testing framework and unit testing stubs  -->  
  118.         <dependency>  
  119.             <groupId>org.springframework</groupId>  
  120.             <artifactId>spring-test</artifactId>  
  121.             <version>${org.springframework.version}</version>  
  122.             <scope>test</scope>  
  123.         </dependency>  
  124.         <!-- Mybatis develop jar -->  
  125.         <dependency>  
  126.             <groupId>org.mybatis</groupId>  
  127.             <artifactId>mybatis-spring</artifactId>  
  128.             <version>1.2.0</version>  
  129.         </dependency>  
  130.   
  131.         <!-- Mybatis and Spring jar, from mybatis -->  
  132.         <dependency>  
  133.             <groupId>org.mybatis</groupId>  
  134.             <artifactId>mybatis</artifactId>  
  135.             <version>3.1.1</version>  
  136.         </dependency>  
  137.         <!-- tomcat servlet develop jar -->  
  138.         <!-- <dependency>  
  139.             <groupId>javax.servlet</groupId>  
  140.             <artifactId>jstl</artifactId>  
  141.             <version>1.1.2</version>  
  142.             <type>pom.lastUpdated</type>  
  143.         </dependency> -->  
  144.         <!-- JSTL tag library -->  
  145.         <dependency>  
  146.             <groupId>javax.servlet</groupId>  
  147.             <artifactId>servlet-api</artifactId>  
  148.             <version>2.5</version>  
  149.             <scope>provided</scope>  
  150.         </dependency>  
  151.         <!-- mysql database driver -->  
  152.         <dependency>  
  153.             <groupId>mysql</groupId>  
  154.             <artifactId>mysql-connector-java</artifactId>  
  155.             <version>5.1.26</version>  
  156.         </dependency>  
  157.         <!--commons-dbcp and commons-pool , Configure Data Source -->  
  158.         <dependency>  
  159.             <groupId>commons-dbcp</groupId>  
  160.             <artifactId>commons-dbcp</artifactId>  
  161.             <version>1.4</version>  
  162.         </dependency>  
  163.         <dependency>  
  164.             <groupId>commons-pool</groupId>  
  165.             <artifactId>commons-pool</artifactId>  
  166.             <version>1.5.6</version>  
  167.         </dependency>  
  168.         <!-- Logging depend on the jar, like log4j,json-lib... -->  
  169.         <dependency>  
  170.             <groupId>commons-logging</groupId>  
  171.             <artifactId>commons-logging-api</artifactId>  
  172.             <version>1.1</version>  
  173.         </dependency>  
  174.         <!-- spring upload files -->  
  175.         <dependency>  
  176.             <groupId>commons-io</groupId>  
  177.             <artifactId>commons-io</artifactId>  
  178.             <version>2.4</version>  
  179.         </dependency>  
  180.           
  181.         <dependency>  
  182.             <groupId>commons-fileupload</groupId>  
  183.             <artifactId>commons-fileupload</artifactId>  
  184.             <version>1.3</version>  
  185.         </dependency>  
  186.         <!-- log log4g -->  
  187.         <dependency>  
  188.             <groupId>log4j</groupId>  
  189.             <artifactId>log4j</artifactId>  
  190.             <version>1.2.17</version>  
  191.         </dependency>  
  192.   
  193.         <!-- dom4j analysis XML files -->  
  194.         <dependency>  
  195.             <groupId>dom4j</groupId>  
  196.             <artifactId>dom4j</artifactId>  
  197.             <version>1.6.1</version>  
  198.         </dependency>  
  199.         <!-- Configuration of the transaction -->  
  200.         <dependency>  
  201.             <groupId>org.aspectj</groupId>  
  202.             <artifactId>aspectjweaver</artifactId>  
  203.             <version>1.7.3</version>  
  204.         </dependency>  
  205.         <dependency>  
  206.             <groupId>aopalliance</groupId>  
  207.             <artifactId>aopalliance</artifactId>  
  208.             <version>1.0</version>  
  209.         </dependency>  
  210.         <dependency>  
  211.             <groupId>cglib</groupId>  
  212.             <artifactId>cglib-nodep</artifactId>  
  213.             <version>2.2</version>  
  214.         </dependency>  
  215.         <!-- json lib -->  
  216.         <dependency>  
  217.             <groupId>net.sf.json-lib</groupId>  
  218.             <artifactId>json-lib</artifactId>  
  219.             <version>2.4</version>  
  220.             <type>pom</type>  
  221.         </dependency>  
  222.         <!-- <dependency>  
  223.             <groupId>commons-beanutils</groupId>  
  224.             <artifactId>commons-beanutils</artifactId>  
  225.             <version>1.8.4-SNAPSHOT</version>  
  226.             <type>pom</type>  
  227.         </dependency> -->  
  228.         <!-- <dependency>  
  229.             <groupId>commons-collections</groupId>  
  230.             <artifactId>commons-collections</artifactId>  
  231.             <version>4.0-SNAPSHOT</version>  
  232.             <type>pom</type>  
  233.         </dependency> -->  
  234.         <dependency>  
  235.             <groupId>commons-lang</groupId>  
  236.             <artifactId>commons-lang</artifactId>  
  237.             <version>2.6</version>  
  238.             <type>pom</type>  
  239.         </dependency>  
  240.         <dependency>  
  241.             <groupId>net.sf.ezmorph</groupId>  
  242.             <artifactId>ezmorph</artifactId>  
  243.             <version>1.0.6</version>  
  244.         </dependency>  
  245.         <!-- junit test -->  
  246.         <dependency>  
  247.             <groupId>junit</groupId>  
  248.             <artifactId>junit</artifactId>  
  249.             <version>3.8.1</version>  
  250.             <scope>test</scope>  
  251.         </dependency>  
  252.   
  253.         <!-- others -->  
  254.         <dependency>  
  255.             <groupId>javax.annotation</groupId>  
  256.             <artifactId>javax.annotation-api</artifactId>  
  257.             <version>1.2</version>  
  258.         </dependency>  
  259.     </dependencies>  
  260.   <build>  
  261.     <finalName>spring_test</finalName>  
  262.   </build>  
  263. </project>  

文件web.xml  路径:WEB-INF下

Xml代码   收藏代码
  1. <!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  2.   
  3. <web-app>  
  4.   <display-name>Archetype Created Web Application</display-name>  
  5.     
  6.   <!-- 配置Mybatis数据库配置文件的位置 如果不指定位置,默认找WEB-INF下applicationContext.xml -->  
  7.   <context-param>  
  8.     <param-name>contextConfigLocation</param-name>  
  9.     <param-value>/WEB-INF/config/app-context.xml</param-value>  
  10.   </context-param>  
  11.     
  12.   <!-- 配置spring转发器 -->  
  13.   <filter>  
  14.     <filter-name>characterEncodingFilter</filter-name>  
  15.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  16.     <init-param>  
  17.         <!-- 编码设置 -->  
  18.         <param-name>encoding</param-name>  
  19.         <param-value>UTF-8</param-value>  
  20.     </init-param>  
  21.     <init-param>  
  22.         <param-name>forceEncoding</param-name>  
  23.         <param-value>true</param-value>  
  24.     </init-param>  
  25.   </filter>  
  26.   <filter-mapping>  
  27.     <filter-name>characterEncodingFilter</filter-name>  
  28.     <url-pattern>/*</url-pattern>  
  29.   </filter-mapping>  
  30.     
  31.   <!-- 添加启动spring监听器 -->  
  32.   <listener>  
  33.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  34.   </listener>  
  35.     
  36.   <servlet>  
  37.       <servlet-name>exam</servlet-name>  
  38.       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  39.       <init-param>  
  40.       <param-name>contextConfigLocation</param-name>  
  41.       <!-- 配置spring-servlet.xml位置 如果不指定位置,默认找WEB-INF下 项目名-servlet.xml  -->  
  42.         <param-value>/WEB-INF/config/spring-servlet.xml</param-value>  
  43.       </init-param>  
  44.   </servlet>  
  45.   <servlet-mapping>  
  46.       <servlet-name>exam</servlet-name>  
  47.       <url-pattern>/</url-pattern>  
  48.   </servlet-mapping>  
  49.     
  50.   <!-- 首页位置 -->  
  51.   <welcome-file-list>  
  52.       <welcome-file>index.jsp</welcome-file>  
  53.   </welcome-file-list>  
  54. </web-app>  

 

 文件app-context.xml 路径:WEB-INF/config下

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  10.     <bean  
  11.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  12.           
  13.         <!-- 先引用jdbc.properties文件,待用 -->  
  14.         <property name="locations" value="/WEB-INF/config/jdbc.properties" />  
  15.     </bean>  
  16.       
  17.     <!-- 配置数据库 -->  
  18.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  19.         destroy-method="close">  
  20.         <property name="driverClassName" value="${jdbc.driver}" />  
  21.         <property name="url" value="${jdbc.url}" />  
  22.         <property name="username" value="${jdbc.user}" />  
  23.         <property name="password" value="${jdbc.password}" />  
  24.         <!-- data source configuration -->  
  25.         <property name="initialSize" value="60" />  
  26.         <property name="maxActive" value="100" />  
  27.         <property name="maxIdle" value="50" />  
  28.         <property name="minIdle" value="10" />  
  29.     </bean>  
  30.       
  31.     <!-- 指定sqlSessionFactory。官方描述:要创建工厂 bean,放置下面的代码在 Spring 的 XML 配置文件中 -->  
  32.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  33.        <property name="dataSource" ref="dataSource" />  
  34.     </bean>  
  35.       
  36.     <!-- 注入映射器 -->  
  37.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  38.         <property name="basePackage" value="com.mxy.mapper" />  
  39.     </bean>  
  40. </beans>  

 

文件spring-servlet.xml  路径:WEB-INF/config下

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!-- Bean头部 -->  
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:util="http://www.springframework.org/schema/util"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  9.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd                
  10.             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
  11.       
  12.     <!-- 激活@Controller模式  即声明 -->  
  13.     <mvc:annotation-driven />  
  14.       
  15.     <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改,要写到代码的外层 -->  
  16.     <context:component-scan base-package="com.mxy" />  
  17.       
  18.     <!-- 注解式声明 -->  
  19.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  20.   
  21.     <!-- 配置jsp文件前缀后缀  -->  
  22.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="prefix">  
  24.             <value>/jsp/</value>  
  25.         </property>  
  26.         <property name="suffix">  
  27.             <value>.jsp</value>  
  28.         </property>  
  29.     </bean>  
  30. </beans>  

文件jdbc.properties   路径:WEB-INF/config下

Java代码   收藏代码
  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost:3306/shop  
  3. jdbc.user=test  
  4. jdbc.password=123456  

 文件test.sql,这个文件直接在数据库下执行即可

Sql代码   收藏代码
  1. SET FOREIGN_KEY_CHECKS=0;  
  2. drop database if exists test;  
  3. create database test;  
  4. use test;  
  5.   
  6. -- ----------------------------  
  7. -- Table structure for `account`  
  8. -- ----------------------------  
  9. DROP TABLE IF EXISTS `account`;  
  10. CREATE TABLE `account` (  
  11.   `id` int(11) NOT NULL AUTO_INCREMENT,  
  12.   `userName` varchar(100) DEFAULT NULL,  
  13.   `passwordvarchar(100) DEFAULT NULL,  
  14.   `email` varchar(100) DEFAULT NULL,  
  15.   PRIMARY KEY (`id`)  
  16. ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8;  

文件TestController.java 路径:com.mxy.controller

Java代码   收藏代码
  1. package com.mxy.controller;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.annotation.Resource;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.servlet.ModelAndView;  
  13.   
  14. import com.mxy.model.User;  
  15. import com.mxy.service.TestService;  
  16.   
  17. @Controller  
  18. @RequestMapping("/testc")  
  19. public class TestController {  
  20.   
  21.     @Resource  
  22.     private TestService accountService;  
  23.   
  24.     @RequestMapping("/test")  
  25.     public ModelAndView test(HttpServletRequest request,  
  26.             HttpServletResponse response) {  
  27.         Map<String, Object> resultMap = new HashMap<String, Object>();  
  28.         String userName = request.getParameter("username");  
  29.   
  30.         String password = request.getParameter("password");  
  31.         String email = request.getParameter("email");  
  32.   
  33.         User entity = new User();  
  34.         entity.setUserName(userName);  
  35.         entity.setPassword(password);  
  36.         entity.setEmail(email);  
  37.         User user = accountService.select(entity);  
  38.         if (user != null) {  
  39.             return new ModelAndView("error", resultMap);  
  40.         }  
  41.   
  42.         accountService.insert(entity);  
  43.   
  44.         resultMap.put("user", entity);  
  45.         return new ModelAndView("success", resultMap);  
  46.     }  
  47. }  

 文件TestMapper.java 路径:com.mxy.mapper

Java代码   收藏代码
  1. package com.mxy.mapper;  
  2.   
  3. import org.springframework.transaction.annotation.Transactional;  
  4.   
  5. import com.mxy.model.User;  
  6.   
  7. @Transactional  
  8. public interface TestMapper {  
  9.       
  10.     public Integer insert(User entity);  
  11.   
  12.     public User select(User entity);  
  13. }  

 文件TestMapper.xml 路径:com.mxy.mapper

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.mxy.mapper.TestMapper">  
  6.   
  7. <resultMap id="account_Result"  type="com.mxy.model.User">  
  8.     <result property="id" column="id" jdbcType="DECIMAL"/>  
  9.     <result property="username" column="userName" jdbcType="VARCHAR"/>  
  10.     <result property="password" column="password" jdbcType="VARCHAR"/>  
  11.     <result property="email" column="email" jdbcType="DECIMAL"/>  
  12. </resultMap>  
  13.       
  14. <insert id="insert" parameterType="com.mxy.model.User">  
  15.     insert into account(userName,password,email) values(#{userName},#{password},#{email})  
  16. </insert>  
  17.   
  18. <select id="select" parameterType="com.mxy.model.User" resultType="com.mxy.model.User">  
  19.     select * from account where userName=#{userName}  
  20. </select>  
  21.      
  22. </mapper>  

 文件User.java 路径:com.mxy.model

Java代码   收藏代码
  1. package com.mxy.model;  
  2.   
  3. public class User {  
  4.     private int id;  
  5.     private String userName;  
  6.     private String password;  
  7.     private String email;  
  8.   
  9.     public int getId() {  
  10.         return id;  
  11.     }  
  12.   
  13.     public void setId(int id) {  
  14.         this.id = id;  
  15.     }  
  16.   
  17.     public String getUserName() {  
  18.         return userName;  
  19.     }  
  20.   
  21.     public void setUserName(String userName) {  
  22.         this.userName = userName;  
  23.     }  
  24.   
  25.     public String getPassword() {  
  26.         return password;  
  27.     }  
  28.   
  29.     public void setPassword(String password) {  
  30.         this.password = password;  
  31.     }  
  32.   
  33.     public String getEmail() {  
  34.         return email;  
  35.     }  
  36.   
  37.     public void setEmail(String email) {  
  38.         this.email = email;  
  39.     }  
  40.   
  41. }  

 文件TestService.java 路径:com.mxy.service

Java代码   收藏代码
  1. package com.mxy.service;  
  2.   
  3. import com.mxy.model.User;  
  4.   
  5. public interface TestService{  
  6.   
  7.     public Integer insert(User entity);  
  8.   
  9.     public User select(User entity);  
  10. }  

 文件TestServiceImpl.java 路径:com.mxy.service.impl

Java代码   收藏代码
  1. package com.mxy.service.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.mxy.mapper.TestMapper;  
  8. import com.mxy.model.User;  
  9. import com.mxy.service.TestService;  
  10.   
  11. @Service  
  12. public class TestServiceImpl implements TestService {  
  13.       
  14.     @Resource  
  15.     private TestMapper accountMapper;  
  16.   
  17.     public Integer insert(User entity) {  
  18.         // TODO Auto-generated method stub  
  19.         return accountMapper.insert(entity);  
  20.     }  
  21.   
  22.     public User select(User entity) {  
  23.         // TODO Auto-generated method stub  
  24.         return accountMapper.select(entity);  
  25.     }  
  26. }  

文件index.jsp   路径:webapp下

Java代码   收藏代码
  1. <html>  
  2. <body>  
  3. <%  
  4.   request.getRequestDispatcher("/jsp/add.jsp").forward(request,response);  
  5. %>  
  6. </body>  
  7. </html>  

文件error.jsp   路径:webapp/jsp下

Java代码   收藏代码
  1. <%@ page isELIgnored="false"%>   
  2. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. 用户已经存在,<a href="/spring_test">返回</a>  
  11. </body>  
  12. </html>  

文件add.jsp    路径:webapp/jsp下

Java代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <div>  
  11.         <form action="testc/test" method="post">  
  12.             <p>用户名:<input type="text" name="username"></p>  
  13.             <p>密码:<input type="password" name="password"></p>  
  14.             <p>邮箱:<input type="text" name="email"></p>  
  15.             <p><input type="submit" value="添加用户"></p>  
  16.         </form>  
  17.     </div>  
  18. </body>  
  19. </html>  

文件success.jsp    路径:webapp/jsp下

Java代码   收藏代码
  1. <%@ page isELIgnored="false"%>   
  2. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. 恭喜,添加成功!<br />  
  11. 用户名:${user.userName}<br />  
  12. 密码:${user.password}<br />  
  13. 邮箱:${user.email}<br />  
  14. 邮箱:${userName}<br />  
  15. <a href="/spring_test">返回</a><br />  
  16. </body>  
  17. </html>  

 

 

发布后访问本地地址 http://localhost:8080/spring_test  如

 

 

 KO。。。。

 

上面我们提到插入到数据库是乱码,我们可以根据这篇文章解决:http://blog.csdn.net/zht666/article/details/8955952

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值