当前位置: 代码迷 >> VC/MFC >> springmvc mybatis调整
  详细解决方案

springmvc mybatis调整

热度:414   发布时间:2016-05-02 03:57:22.0
springmvc mybatis整合

?

?

首先是pom.xml,下面列出的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>com.tch.test</groupId>	<artifactId>springmvc-mybatis</artifactId>	<packaging>war</packaging>	<version>0.0.1-SNAPSHOT</version>	<name>springmvc-mybatis Maven Webapp</name>	<url>http://maven.apache.org</url>	<dependencies>		<!-- log -->		<dependency>			<groupId>commons-logging</groupId>			<artifactId>commons-logging</artifactId>			<version>1.1.3</version>		</dependency>		<dependency>			<groupId>log4j</groupId>			<artifactId>log4j</artifactId>			<version>1.2.17</version>		</dependency>		<dependency>			<groupId>org.slf4j</groupId>			<artifactId>slf4j-log4j12</artifactId>			<version>1.7.6</version>		</dependency>		<!-- spring -->		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-context</artifactId>			<version>${springframework.version}</version>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-context-support</artifactId>			<version>${springframework.version}</version>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-webmvc</artifactId>			<version>${springframework.version}</version>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-orm</artifactId>			<version>${springframework.version}</version>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-web</artifactId>			<version>${springframework.version}</version>		</dependency>		<dependency>			<groupId>org.springframework.security</groupId>			<artifactId>spring-security-web</artifactId>			<version>3.2.5.RELEASE</version>		</dependency>		<dependency>			<groupId>org.aspectj</groupId>			<artifactId>aspectjweaver</artifactId>			<version>1.8.2</version>		</dependency>		<dependency>			<groupId>jstl</groupId>			<artifactId>jstl</artifactId>			<version>1.2</version>		</dependency>		<!-- validation -->		<dependency>			<groupId>org.hibernate</groupId>			<artifactId>hibernate-validator</artifactId>			<version>4.3.0.Final</version>		</dependency>		<!-- velocity -->		<dependency>			<groupId>org.apache.velocity</groupId>			<artifactId>velocity</artifactId>			<version>1.7</version>		</dependency>		<dependency>			<groupId>org.apache.velocity</groupId>			<artifactId>velocity-tools</artifactId>			<version>2.0</version>		</dependency>		<!-- json -->		<dependency>			<groupId>org.codehaus.jackson</groupId>			<artifactId>jackson-mapper-asl</artifactId>			<version>1.9.13</version>		</dependency>		<dependency>			<groupId>com.googlecode.json-simple</groupId>			<artifactId>json-simple</artifactId>			<version>1.1.1</version>		</dependency>		<dependency>			<groupId>com.fasterxml.jackson.core</groupId>			<artifactId>jackson-core</artifactId>			<version>2.5.1</version>		</dependency>		<dependency>			<groupId>com.fasterxml.jackson.core</groupId>			<artifactId>jackson-databind</artifactId>			<version>2.5.1</version>		</dependency>		<dependency>			<groupId>com.fasterxml.jackson.core</groupId>			<artifactId>jackson-annotations</artifactId>			<version>2.5.1</version>		</dependency>		<dependency>			<groupId>com.fasterxml.jackson.module</groupId>			<artifactId>jackson-module-afterburner</artifactId>			<version>2.5.1</version>		</dependency>		<!-- mybatis -->		<dependency>			<groupId>org.mybatis</groupId>			<artifactId>mybatis</artifactId>			<version>3.2.7</version>		</dependency>		<dependency>			<groupId>org.mybatis</groupId>			<artifactId>mybatis-spring</artifactId>			<version>1.2.2</version>		</dependency>		<dependency>			<groupId>commons-dbcp</groupId>			<artifactId>commons-dbcp</artifactId>			<version>1.4</version>		</dependency>		<dependency>			<groupId>mysql</groupId>			<artifactId>mysql-connector-java</artifactId>			<version>5.1.9</version>		</dependency>		<!-- provided -->		<dependency>			<groupId>javax.servlet.jsp</groupId>			<artifactId>javax.servlet.jsp-api</artifactId>			<version>2.3.1</version>			<scope>provided</scope>		</dependency>		<dependency>			<groupId>javax.servlet</groupId>			<artifactId>javax.servlet-api</artifactId>			<version>3.1.0</version>			<scope>provided</scope>		</dependency>		<dependency>			<groupId>org.projectlombok</groupId>			<artifactId>lombok</artifactId>			<version>1.12.6</version>			<scope>provided</scope>		</dependency>	</dependencies>	<properties>		<junit-version>3.8.1</junit-version>		<spring-version>3.2.6.RELEASE</spring-version>		<hiberante-version>3.6.10.Final</hiberante-version>		<javassist-version>3.12.0.GA</javassist-version>		<mysql-version>5.1.28</mysql-version>		<jackson-version>1.9.10</jackson-version>		<log4j-version>1.2.17</log4j-version>		<slf4j-version>1.7.5</slf4j-version>		<springframework.version>4.0.3.RELEASE</springframework.version>		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>	</properties>	<build>		<finalName>springmvc-mybatis</finalName>	</build></project>

?

?

?

web.xml(匹配URL格式为 : /mvc/xxx的格式):

?

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">	<!-- 配置spring mvc的核心控制器DispatcherServlet -->	<servlet>		<servlet-name>spring_mvc</servlet-name>		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>		<!-- 默认情况下,spring mvc配置文件在/WEB-INF/下,servlet名字-servlet.xml(这里是spring_mvc-servlet.xml) -->		<init-param>		    <!-- 指定spring mvc配置文件位置 -->			<param-name>contextConfigLocation</param-name>			<param-value>classpath*:dispatcher-servlet.xml</param-value>		</init-param>		<load-on-startup>1</load-on-startup>	</servlet>	<servlet-mapping>		<servlet-name>spring_mvc</servlet-name>		<url-pattern>/mvc/*</url-pattern>	</servlet-mapping>	<welcome-file-list>		<welcome-file>index.jsp</welcome-file>	</welcome-file-list></web-app>

?

?

dispatcher-servlet.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:tx="http://www.springframework.org/schema/tx"	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.0.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd      http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">	<!-- 启动扫描component功能 -->	<context:component-scan base-package="com.tch.test.springmvcmybatis" />	<!-- viewResolver -->	<bean		class="org.springframework.web.servlet.view.InternalResourceViewResolver">		<property name="prefix" value="/WEB-INF/pages/" />		<property name="suffix" value=".jsp" />	</bean>	<mvc:annotation-driven />	<!-- 拦截器 -->	<!-- <mvc:interceptors>		这里配置的拦截器相当于全局拦截器,只要有响应的后端处理器,就会经过该拦截器		<bean class="com.tch.test.springmvcmybatis.interceptor.MyInteceptor" />		<mvc:interceptor>			只拦截匹配的路径			<mvc:mapping path="/namespace/*" />			<bean class="com.tch.test.springmvcmybatis.interceptor.MyInteceptor2" />		</mvc:interceptor>	</mvc:interceptors> -->	<!-- 启动注解实物配置功能 -->	<tx:annotation-driven transaction-manager="transactionManager" />	<!-- 数据源 -->	<bean id="dataSource"		class="org.springframework.jdbc.datasource.DriverManagerDataSource">		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>		<property name="url" value="jdbc:mysql://localhost:3306/test"></property>		<property name="username" value="root"></property>		<property name="password" value="root"></property>	</bean>	<!-- 事务管理器 -->	<bean id="transactionManager"		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">		<property name="dataSource" ref="dataSource" />	</bean>	<!--读取数据库配置文件 -->	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">		<property name="dataSource" ref="dataSource" />		<!-- 如果mapper的xml文件名字和mapper接口的名字一致,并且在同一目录下(例如UserMapper.java和UserMapper.xml),可以不配置mapperLocations, -->		<property name="mapperLocations" value="classpath:mybatis/**/*Mapper.xml" />		<!-- 指定别名,在mapper的xml文件中可以使用别名(例如User/user来代表com.tch.test.spring_mybatis.entity.User),提高开发效率 -->		<!-- <property name="typeAliasesPackage" value="com.tch.test.springmvcmybatis.bean" /> -->		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>	</bean>	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">		<!-- 指定mapper接口包名,可以通过动态代理生成代理类,这样就可以在service层直接通过注入的方式进行dao操作了 -->		<property name="basePackage" value="com.tch.test.springmvcmybatis.mapper" />		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />	</bean></beans>

?

?

log4j.properties:

?

log4j.rootLogger=debug,CONSOLE#################### Console Appender###################log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.Threshold=debuglog4j.appender.CONSOLE.Target=System.outlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p (%c:%L) - %m%n

?

?

sqlMapConfig.xml:

?

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>  <settings>    <setting name="lazyLoadingEnabled" value="true" />    <setting name="aggressiveLazyLoading" value="false" />  </settings></configuration> 

?

?

?

BlogMapper.xml(该mapper文件位于 resource/mybatis/mapper文件夹下):

?

<?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"><!-- 这里的namespace是mapper接口的全路径 --><mapper namespace="com.tch.test.springmvcmybatis.mapper.BlogMapper">	<!-- 下面的select/insert/delete/update 的id和mapper接口的方法名一致,parameterType和方法的输入参数一致,resultType和方法的返回值一致 -->	<!-- where -->	<select id="getBlog" resultType="com.tch.test.springmvcmybatis.bean.Blog">		SELECT * FROM blog		<!-- where: where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 			元素也知道如何将他们去除 -->		<!-- <where>			<if test="status != null">				AND status = #{status}			</if>			<if test="title != null">				AND title = #{title}			</if>			<if test="author != null">				AND author = #{author}			</if>		</where> -->	</select></mapper>

?

?

?

Blog.java:

?

package com.tch.test.springmvcmybatis.bean;public class Blog {	private String state;	private String title;	private String author;	public String getState() {		return state;	}	public void setState(String state) {		this.state = state;	}	public String getTitle() {		return title;	}	public void setTitle(String title) {		this.title = title;	}	public String getAuthor() {		return author;	}	public void setAuthor(String author) {		this.author = author;	}	@Override	public String toString() {		return "Blog [state=" + state + ", title=" + title + ", author="				+ author + "]";	}}

?

?

controller:

?

package com.tch.test.springmvcmybatis.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.tch.test.springmvcmybatis.service.BlogService;[email protected]("prototype")				//这样可以让控制器是原型模式,而不是单例(spring mvc建议controller使用单例) @Controller("helloWorld")// 表示这个类是后端控制器@RequestMapping("/namespace")// [email protected],则该类下面的方法的访问路径都需要加上该前缀,类似struts2的namespacepublic class HelloWorldController {	@Autowired	private BlogService blogService;	@RequestMapping("/getBlog")	public String getBlog(Model model) {		model.addAttribute("message",				blogService.getBlog("ACTIVE", "dreamoftch-1", "title-1"));		return "helloWorld";	}}

?

?

package com.tch.test.springmvcmybatis.service;import java.util.List;import org.apache.ibatis.annotations.Param;import com.tch.test.springmvcmybatis.bean.Blog;public interface BlogService {	List<Blog> getBlog(@Param("status") String status, @Param("author") String author, @Param("title") String title);	}

?

package com.tch.test.springmvcmybatis.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.tch.test.springmvcmybatis.bean.Blog;import com.tch.test.springmvcmybatis.mapper.BlogMapper;@Service@Transactionalpublic class BlogServiceImpl implements BlogService{	@Autowired	private BlogMapper blogMapper;		public List<Blog> getBlog(String status, String author, String title) {		return blogMapper.getBlog();	}	}

?

?

package com.tch.test.springmvcmybatis.mapper;import java.util.List;import com.tch.test.springmvcmybatis.bean.Blog;public interface BlogMapper {	//	List<Blog> getBlog(@Param("status") String status, @Param("author") String author, @Param("title") String title);		List<Blog> getBlog();	}

?

跑起来之后,通过URL:

?

?localhost:8080/springmvc-mybatis/mvc/namespace/getBlog

?

即可访问。

?

OK。

?

?

  相关解决方案