mybatis3.2.6 spring 4を統合する.0とspringMVC 4.0開発
1.新規プロジェクト、jarパッケージのインポート
eclipse/myeclipseにWebプロジェクトを新規作成し、下図に示すjarパッケージをインポートします.jarパッケージの統合リソースは
mavenを使用すると、構成がより便利になります.
2.webを構成する.xmlファイル
3.ログファイルの構成
classpathパスの下にファイルlog 4 jを作成する.properties、中に記入します
4.spring-mybatis統合ファイルの構成
5.model、mappingファイルの構成
まず、データベーステストテーブルを構成します.ここでは、モバイル・エンド・バックグラウンドのユーザー・テーブルを例に挙げます.データベースにテーブルが作成されているため、$MYSQL_に移動できます.HOME/binディレクトリの下でコマンド:
データベースのテーブル作成が完了したら、pojoファイル、xmlなどのファイルを作成します.ここでは、手動で構成する方法と、データベースを逆方向に生成する方法の2つがあります.
1.手動構成対応するpojoクラス を先に作成するマッピングインタフェースjavaファイル を作成する対応するUserMapperを作成する.xmlファイル
ここで注意しなければならないのは、spring-mybatisに対応する開発規範の問題です.xmlスキャン構成、UserMapper.JAvaファイルはclasspath:com/yc/daoの下に保存され、UserMapper.xmlはclasspath:com/yc/mappingの下に保管するとともに、UserMapperに特に注意する.xmlファイルのnamespaceカバーとUserMapper.JAvaファイルに対応する、本例の
2.データベース構築テーブルによる逆生成
生成ツールをダウンロードして解凍し、generatorを構成することができます.xmlファイル、構成例は次のとおりです.
注意あなたのダウンロードパスによって正しいデータベースドライバパッケージの位置を配置し、targetProjectなどの属性に対応する配置は、必ずしもあなたのプロジェクトで任意の位置に作成する必要はありません.それからインポートをコピーし、配置した後、現在のディレクトリの下でコマンドラインで
6.junit 4を使用して構成をテストする
テストファイルを作成するには
7.spring-mvcを構成する.xmlファイル
8.サービス層とコントロール層ファイルの構成
9.springMVCのテスト
プロジェクトをtomcatサーバに配置し、遊覧機を開き、
eclipse/myeclipseにWebプロジェクトを新規作成し、下図に示すjarパッケージをインポートします.jarパッケージの統合リソースは
http://download.csdn.net/detail/qwe6112071/9467007
にダウンロードできます.mavenを使用すると、構成がより便利になります.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- , -->
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- java ee jar -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- Mysql jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- dbcp jar , applicationContext.xml -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- , -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
</dependencies>
2.webを構成する.xmlファイル
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_3_0.xsd" version="3.0">
<display-name>demo</display-name>
<!-- servlet tomcat,jetty , / /static/ , http://localhost/foo.css , http://localhost/static/foo.css -->
<!-- -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/images/*</url-pattern>
<url-pattern>/fonts/*</url-pattern>
</servlet-mapping>
<!-- Spring mybatis -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-*.xml</param-value>
</context-param>
<!-- -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<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:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- *.do, struts -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.ログファイルの構成
classpathパスの下にファイルlog 4 jを作成する.properties、中に記入します
log4j.rootLogger=DEBUG,Console,File
#
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
# ,
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#
log4j.appender.File = org.apache.log4j.RollingFileAppender
#
log4j.appender.File.File = logs/ssm.log
#
log4j.appender.File.MaxFileSize = 10MB
# , DEBUG DEBUG
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
4.spring-mybatis統合ファイルの構成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- -->
<context:component-scan base-package="com.yc" />
<!-- -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- -->
<property name="initialSize" value="${initialSize}"></property>
<!-- -->
<property name="maxActive" value="${maxActive}"></property>
<!-- -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- -->
<property name="minIdle" value="${minIdle}"></property>
<!-- -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring MyBatis , mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- sqlSessionFactory SQL -->
<property name="dataSource" ref="dataSource" />
<!-- mapping.xml , Configuration.xml -->
<property name="mapperLocations" value="classpath*:com/yc/mapping/*Mapper.xml"></property>
</bean>
<!-- DAO ,Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yc.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- ( )transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" /> </bean>
<!-- -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.yc.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
</beans>
5.model、mappingファイルの構成
まず、データベーステストテーブルを構成します.ここでは、モバイル・エンド・バックグラウンドのユーザー・テーブルを例に挙げます.データベースにテーブルが作成されているため、$MYSQL_に移動できます.HOME/binディレクトリの下でコマンド:
mysqldump -u -p
を使用して、mysqldump -uyc -pyc yc User > /home/user.sql
のような対応するテーブル構造を対応するディレクトリに対してuserを開く.sqlファイルでは、次のようなテーブルsql文が得られます.DROP TABLE IF EXISTS `User`;
CREATE TABLE `User` ( `id` int(11) NOT NULL AUTO_INCREMENT, `headImg` varchar(255) DEFAULT NULL, `isDeleted` int(11) NOT NULL DEFAULT '0', `name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `phoneNum` varchar(255) DEFAULT NULL, `userType` varchar(255) DEFAULT NULL, `openId` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
#
LOCK TABLES `User` WRITE;
/*!40000 ALTER TABLE `User` DISABLE KEYS */;
INSERT INTO `User` VALUES (12,'http://localhost:8090/dheadImg/1.jpg',0,'glcnk','$2a$10$EpmkvDhQU5AHkn3Mvq1oDuJbT83aZRooqF0ZxdawiQsTz/sWfpxJa','1234','normal',NULL),(13,'http://localhost:8090/dheadImg/1.jpg',0,'psnys','$2a$10$dv4EQjbMwmUpbNB.kFySNe9RRJ1jwAXliK4FotoRLDrdfSAyWkk7C','3412','normal',NULL);
UNLOCK TABLES;
データベースのテーブル作成が完了したら、pojoファイル、xmlなどのファイルを作成します.ここでは、手動で構成する方法と、データベースを逆方向に生成する方法の2つがあります.
1.手動構成
package com.yc.model;
public class User {
private Integer id;
private String headimg;
private Integer isdeleted;
private String name;
private String password;
private String phonenum;
private String usertype;
private String openid;
//
@Override
public String toString() {
return "User [id=" + id + ", headimg=" + headimg + ", isdeleted="
+ isdeleted + ", name=" + name + ", password=" + password
+ ", phonenum=" + phonenum + ", usertype=" + usertype
+ ", openid=" + openid + "]";
}
getter and setter here.....
}
package com.yc.dao;
import com.yc.model.User;
public interface UserMapper {
User selectByPrimaryKey(Integer id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yc.dao.UserMapper" ><!-- UserMapper -->
<resultMap id="BaseResultMap" type="com.yc.model.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="headImg" property="headimg" jdbcType="VARCHAR" />
<result column="isDeleted" property="isdeleted" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="phoneNum" property="phonenum" jdbcType="VARCHAR" />
<result column="userType" property="usertype" jdbcType="VARCHAR" />
<result column="openId" property="openid" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, headImg, isDeleted, name, password, phoneNum, userType, openId
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from User
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
ここで注意しなければならないのは、spring-mybatisに対応する開発規範の問題です.xmlスキャン構成、UserMapper.JAvaファイルはclasspath:com/yc/daoの下に保存され、UserMapper.xmlはclasspath:com/yc/mappingの下に保管するとともに、UserMapperに特に注意する.xmlファイルのnamespaceカバーとUserMapper.JAvaファイルに対応する、本例の
com.yc.model.User
のように、ここで構成が間違っている場合、テスト時にnested exception is orgというエラーメッセージが表示されます.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [/home/myBatisYc/target/classes/com/yc/mapping/UserMapper.xml]’; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ‘com.yc.User’. Cause: java.lang.ClassNotFoundException: Cannot find class: com.yc.User 2.データベース構築テーブルによる逆生成
生成ツールをダウンロードして解凍し、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>
<!-- -->
<classPathEntry location="/home/zenghao/ /generator/mysql-connector-java-5.1.34.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://localhost:3306/yc" userId="root" password="root">
<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- -->
<javaModelGenerator targetPackage="com.yc.model" targetProject="/home/zenghao/mybatis">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- -->
<sqlMapGenerator targetPackage="com.yc.mapping" targetProject="/home/zenghao/mybatis">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- DAO -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yc.dao" targetProject="/home/zenghao/mybatis">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- ( tableName domainObjectName ) -->
<table tableName="Article" domainObjectName="BackArticle" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="User" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
注意あなたのダウンロードパスによって正しいデータベースドライバパッケージの位置を配置し、targetProjectなどの属性に対応する配置は、必ずしもあなたのプロジェクトで任意の位置に作成する必要はありません.それからインポートをコピーし、配置した後、現在のディレクトリの下でコマンドラインで
http://download.csdn.net/detail/qwe6112071/9467019
を実行すればいいです.6.junit 4を使用して構成をテストする
テストファイルを作成するには
package com.yc.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yc.dao.UserMapper;
import com.yc.model.User;
public class Test1 {
private UserMapper userMapper;
/** * before , * Junit * before ApplicationContext usermapper */
@Before
public void before(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-mybatis.xml");
userMapper = (UserMapper) ac.getBean("userMapper");
}
@Test
public void test(){
User user = userMapper.selectByPrimaryKey(12);
System.out.println(user);
/* * , : * User [id=12, headimg=http://localhost:8090/dheadImg/1.jpg, isdeleted=0, name=glcnk, password=$2a$10$EpmkvDhQU5AHkn3Mvq1oDuJbT83aZRooqF0ZxdawiQsTz/sWfpxJa, phonenum=1234, usertype=normal, openid=null] */
}
/* * , 。 */
@After
public void after(){
userMapper = null;
ac = null;
}
}
7.spring-mvcを構成する.xmlファイル
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- spring mvc -->
<mvc:annotation-driven />
<!-- , SpringMVC @controller -->
<context:component-scan base-package="com.yc.controller" />
<context:component-scan base-package="com.yc.service" />
<!-- IE AJAX , JSON -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- SpringMVC , POJO -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON -->
</list>
</property>
</bean>
<!-- , -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- action return , url -->
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- , , , -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean> -->
</beans>
8.サービス層とコントロール層ファイルの構成
package com.yc.service;
import com.yc.model.User;
public interface IUserService {
User selectByPrimaryKey(Integer id);
}
/*-------------------------- -------------------------*/
package com.yc.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yc.dao.UserMapper;
import com.yc.model.User;
@Service
public class UserService implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
}
/*-------------------------- -------------------------*/
package com.yc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yc.model.User;
import com.yc.service.IUserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("getUser")
@ResponseBody// json
public User getUser(Integer id){
return userService.selectByPrimaryKey(id);
}
}
9.springMVCのテスト
プロジェクトをtomcatサーバに配置し、遊覧機を開き、
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
にアクセスします.遊覧機にUserシーケンス化されたjsonフォーマット文字列が印刷されているのが見えます.http://localhost:8080/myBatisYc/user/getUser?id=13
ここでmybatis 3.2.6 spring 4とspringMVC 4の統合が完了しました