`
dreamoftch
  • 浏览: 498337 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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.ConsoleAppender
log4j.appender.Threshold=debug
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.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>
	<insert id="addBlog" parameterType="com.tch.test.springmvcmybatis.bean.Blog">
		insert into blog(title, status, author) values(#{title}, #{status},
		#{author})
		<selectKey keyProperty="id" resultType="Integer" order="AFTER">
			select LAST_INSERT_ID() AS ID
		</selectKey>
	</insert>

</mapper>

 

 

 

 

Blog.java:

package com.tch.test.springmvcmybatis.bean;

public class Blog {

	private Integer id;
	private String status;
	private String title;
	private String author;

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	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;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "Blog [id=" + id + ", status=" + status + ", 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.bean.Blog;
import com.tch.test.springmvcmybatis.service.BlogService;

//@Scope("prototype")				//这样可以让控制器是原型模式,而不是单例(spring mvc建议controller使用单例) 
@Controller("helloWorld")
// 表示这个类是后端控制器
@RequestMapping("/namespace")
// 在类上面加上@RequestMapping,则该类下面的方法的访问路径都需要加上该前缀,类似struts2的namespace
public 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";
	}
	
	@RequestMapping("/addBlog")
	public String addBlog(Model model, Blog blog) {
		blogService.addBlog(blog);
		model.addAttribute("blog", blog);
		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);

	void addBlog(Blog blog);
}

 

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
@Transactional
public class BlogServiceImpl implements BlogService{

	@Autowired
	private BlogMapper blogMapper;
	
	
	public List<Blog> getBlog(String status, String author, String title) {
		return blogMapper.getBlog();
	}

	@Transactional(rollbackFor=Exception.class)
	public void addBlog(Blog blog) {
		blogMapper.addBlog(blog);
	}

	
}

 

 

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();
	
	void addBlog(Blog blog);
	
}

 

 

helloWorld.jsp:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'User.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  <body>
    message:${message}
    <br/>
    blog: ${blog}
  </body>
</html>

 

 

 

跑起来之后,通过URL:

 

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

 

 http://localhost:8080/springmvc-mybatis/mvc/namespace/addBlog?status=ready&title=title-111&author=dreamoftch

 

即可访问。

 

OK。

 

 

分享到:
评论

相关推荐

    spring springmvc mybatis 整合jar包

    spring springmvc mybatis的整合jar包,以及mysql,Junit,fastJson等的jar包,导入这些jar包 搭一个SSM的环境,可以使用SSM框架做一个简单的能够实现增删改查的小项目,具体的jar包及其版本如下 : aopalliance-1.0....

    Spring SpringMVC Mybatis 整合jar包

    这个“Spring SpringMVC Mybatis整合jar包”应该包含了所有必要的库文件,如Spring的核心库、SpringMVC的库、Mybatis的库,以及可能的数据库驱动等。开发者只需将这些jar包添加到项目的类路径中,就可以快速搭建起一...

    Spring SpringMVC Mybatis 整合 Redis

    Spring SpringMVC Mybatis 整合 Redis .........................................................................................

    Spring+SpringMVC+Mybatis框架项目整合

    "Spring+SpringMVC+Mybatis框架项目整合"是一个典型的后端技术栈,旨在提供高效、灵活且可维护的解决方案。下面将详细介绍这三个框架及其整合过程中的关键知识点。 **Spring框架**: Spring是一个开源的Java平台,...

    spring springmvc mybatis整合

    使用myeclipse整合的spring springmvc mybatis含有整合所需要的jar包;已经测试过tomcat能正常启动;还包含了springmvc的一些页面跳转!!感兴趣的可以下载参考!

    springboot springmvc mybatis整合

    整合SpringBoot、SpringMVC和MyBatis的过程如下: 1. **引入依赖**:在SpringBoot的pom.xml文件中,我们需要添加Spring Boot Starter Web(包含SpringMVC)、Spring Boot Starter Data JPA(用于数据库操作)以及...

    Spring+SpringMVC+Mybatis框架整合源码

    标题中的“Spring+SpringMVC+Mybatis框架整合源码”指的是一个基于Java的Web开发项目,它结合了三个主流的开源框架:Spring、SpringMVC和Mybatis,以实现高效且灵活的企业级应用开发。这三种框架在Java世界中扮演着...

    springmvc maven mybatis整合

    在"springmvc maven mybatis整合"的项目中,Maven负责管理Spring、SpringMVC、MyBatis以及相关的数据库驱动等库的版本和依赖。 MyBatis是一个轻量级的持久层框架,它将SQL语句与Java代码解耦,通过XML或注解方式...

    springMVC+mybatis整合demo

    完成上述步骤后,一个基本的SpringMVC+MyBatis整合项目就搭建完成了。这样的架构提供了松耦合、可测试性以及灵活的数据库操作,便于开发和维护大型Web应用。在实际项目中,我们还可以进一步优化,比如引入Spring ...

    springMVC mybatis整合实例

    SpringMVC、Spring和MyBatis是Java Web开发中非常流行的三大框架,它们共同构建了SSM(Spring、SpringMVC、MyBatis)集成框架,用于实现高效、灵活且可扩展的企业级应用。下面我们将深入探讨这三大框架以及它们在...

    spring springmvc mybatis 整合demo

    在Java Web开发中,"Spring + SpringMVC + MyBatis"(简称SSM)是一个常见的框架整合,用于构建高效、灵活的企业级应用程序。这个整合Demo涵盖了Spring框架的核心功能、SpringMVC作为Web层的处理机制,以及MyBatis...

    spring springmvc mybatis 整合学习

    在Java Web开发中,Spring、SpringMVC和MyBatis是三个非常重要的框架,它们的组合被称为SSM(Spring、SpringMVC、MyBatis)框架。SSM框架的整合可以构建出高效、灵活的Web应用程序。下面我们将详细探讨这三个框架...

    Spring SpringMVC MyBatis整合包

    整合Spring、SpringMVC和MyBatis的目的是构建一个完整的、松散耦合的Web应用。在整合过程中,通常会利用Spring的DI来管理MyBatis的SqlSessionFactory和Mapper接口,SpringMVC则作为请求处理的入口。通过Spring的配置...

    Spring-SpringMVC-Mybatis整合所有jar包

    这个压缩包“Spring-SpringMVC-Mybatis整合所有jar包”包含了这三个框架整合所需的全部依赖,使得开发者能够快速搭建起一个功能完备的后端服务。 1. **Spring框架**:Spring是一个全面的开源应用框架,它提供了对...

    spring springmvc mybatis 整合

    SSM框架,即Spring、SpringMVC和MyBatis的集成,是Java开发中的常见Web应用框架组合。这个整合过程涉及到三个主要组件的配置和协同工作,以实现数据持久化、业务逻辑处理以及用户界面交互。 Spring框架是核心,它...

    Spring+SpringMVC+Mybatis整合

    SSM指的是Spring+SpringMVC+MyBatis集成的一个Web应用程序开源框架,个人整合SSM的源码以及笔记,该有的注释都写的很详细了,应该能看懂,之前发布过一个,但是因为是自己写的生成代码生成的mapper,有点错误,在...

    mybatis3+spring+springMVC4整合jar包.rar

    标题 "mybatis3+spring+springMVC4整合jar包.rar" 描述的是一个整合了MyBatis 3、Spring 4 和 Spring MVC 4 的项目压缩包。这个压缩包通常用于快速搭建一个基于Java的Web开发环境,尤其是用于处理数据库操作和前端...

    Spring、SpringMVC、Mybatis整合所需最新jar包

    SSM整合是Java Web开发中常见的一种框架组合,由Spring、SpringMVC和Mybatis三大组件构成。这三大框架的整合使得开发人员可以更高效地处理业务逻辑、控制层交互和数据持久化。以下是关于这三个框架及其整合的知识点...

    SpringMVC+myBatis整合jar包.zip

    整合SpringMVC和MyBatis,主要是为了实现Spring对MyBatis的管理,包括事务控制、依赖注入等。这通常涉及到以下步骤: 1. 配置数据源:在Spring的配置文件中定义数据源,这是MyBatis连接数据库的基础。 2. 创建...

    springMVC+mybatis整合及测试(亲测可运行)

    总的来说,SpringMVC和Mybatis的整合使得开发者能够更专注于业务逻辑的实现,而不必过多地关注底层的数据访问细节,提高了开发效率和代码的可维护性。通过亲测可运行的示例,学习者可以快速上手并理解这两个框架的...

Global site tag (gtag.js) - Google Analytics