SpringMVC4 + Spring + MyBatis3 基于注解的最简配置

SpringMVC4 + Spring + MyBatis3 基于注解的最简配置

本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建。

1. web.xml

我们知道springmvc是基于DispatcherServlet来分发请求的,所以先在web.xml文件中搭建DispatcherServlet,还有spring的监听器:

<?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">
  <display-name>sp</display-name>
  <servlet>
	  <servlet-name>dispatcherServlet</servlet-name>
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <load-on-startup>1</load-on-startup>
	  <init-param>
		  <param-name>contextConfigLocation</param-name>
		  <param-value>classpath:config/spring-mvc.xml</param-value>
	  </init-param>
  </servlet>
  <servlet-mapping>
	  <servlet-name>dispatcherServlet</servlet-name>
	  <url-pattern>/</url-pattern>
  </servlet-mapping>
  <listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
	  <param-name>contextConfigLocation</param-name>
	  <param-value>classpath:config/applicationContext.xml</param-value>
  </context-param>
</web-app>

servlet下面的init-param中的指定了springmvc的dispatcherServlet的配置文件:config/spring-mvc.xml,所有springmvc相关的都在该文件中进行配置。在DispatcherServlet(其父类)中使用: getServletConfig().getInitParameter("paramName");  可以访问到init-param中指定的参数,从而可以读取到config/spring-mvc.xml文件。load-on-startup值为1指定了dispatcherServlet随servlet容器启动。

ContextLoaderListener是spring监听servlet容器的启动的,在servlet容器启动时,就初始化bean工厂,对bean进行初始化等等操作。context-param指定了spring的配置文件config/applicationContext.xml,可以使用: getServletContext().getInitParameter("paraName"); 读取到值。

2. springmvc.xml

下面我们看一下springmvc.xml该如何配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="net.aazj.controller" />
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 <property name="prefix" value="/" />
	 <property name="suffix" value=".jsp" />
     </bean>
     // ... ...
</beans>

启用注解驱动,并指定control的包路径,还有指定了视图解析器,so easy。

3. applicationContext.xml

spring中相关bean扫描,事物的配置,以及和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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="net.aazj.service" />
	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config/db.properties" />
	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	  <property name="dataSource" ref="dataSource" />
	  <property name="configLocation" value="classpath:config/mybatis-config.xml" />
	  <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
	</bean>
	<!-- Transaction manager for a single JDBC DataSource -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" /> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	  <property name="basePackage" value="net.aazj.mapper" />
	</bean>
</beans>

同样相关service bean也使用基于注解的扫描方式:context:component-scan,所以需要在serviceImpl相关类上和方法上使用@Transanctional注解类配置事物。

sqlSessionFactory的配置相当重要,configLocation指定了mybatis的配置文件,如果需要在mybatis配置文件中配置比如<settings>, <typeAliases>, <mappers>则,需要在这里指定,如果不需要就没有必要指定值了。mapperLocations指定了mapper接口映射sql语句的xml文件的位置。MapperScannerConfigurer指定了mapper接口所在的包路径。

4. mybatis-config.xml

spring和mybatis的接口,其实可以不需要mybatis-config.xml文件的存在,只有在需要配置<settings>, <typeAliases>, <mappers>(其实mapper也一并也是在applicationContext.xml中进行配置)才需要mybatis-config.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="cacheEnabled" value="true"/>
	  <setting name="lazyLoadingEnabled" value="true"/>
	  <setting name="multipleResultSetsEnabled" value="true"/>
	  <setting name="useColumnLabel" value="true"/>
	  <setting name="useGeneratedKeys" value="false"/>
	  <setting name="autoMappingBehavior" value="PARTIAL"/>
	  <setting name="defaultExecutorType" value="SIMPLE"/>
	  <setting name="defaultStatementTimeout" value="25"/>
	  <setting name="safeRowBoundsEnabled" value="false"/>
	  <setting name="mapUnderscoreToCamelCase" value="false"/>
	  <setting name="localCacheScope" value="SESSION"/>
	  <setting name="jdbcTypeForNull" value="OTHER"/>
	  <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
  </settings>
  <typeAliases>
	  <package name="net.aazj.pojo"/>
  </typeAliases>
</configuration>

<settings>指定了数据库操作相关的设置,typeAliases指定了可以给数据库表对应的类所在的包路径,可以在sql的xml使用它们的别名:

package net.aazj.pojo;

import org.apache.ibatis.type.Alias;

@Alias("User")
public class User {

    private Integer id;

    private String name;

        // ... ...

}

在xml文件中使用别名 User 来代替:net.aazj.pojo.User

<?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="net.aazj.mapper.UserMapper">
	 <cache />
	<select id="getUser" resultType="User">
		select * from user where id = #{id}
	</select>
	<select id="addUser" parameterType="string">
		insert into user(name) values(#{name})
	</select>
</mapper>

这里 resultType="User" 不需要使用全限定类名。<cache />启用了基于namespace="net.aazj.mapper.UserMapper"的全局缓存。

/***********************基本描述**********************************/ 0、根据表可以单独生成javaBean后缀可以自定义 1、工具本身是非常简单的,每个人都能做就是使用模板替换生成相应文件 2、工具主要针对SpringMvc+Mybatis注解+Mysql生成对象,dao、sqlDao、interface、实现接口 3、根据表生成Excel 4、生成成功后倒入到自己对应的项目中,然后Ctrl+Shipt+O(Eclipse快速倒入包)实现 5、里面因为运用的是注解,所以很多包我就没有提供了因为这些都是很基础的东西,不会的同学可以去网上查看搭建Mybatis注解 6、生成了些什么,具体主要是对单表的增、删、改、查(分页) /********************************/ /********************************/ /*************完全免费***********/ /********************************/ /********************************/ 如果大家喜欢可以再给我提其他功能,有时间我加上 /*********************************************************************************/ 模板介绍: MySql.Data.dll :连接Mysql基本dl我们的的驱动。 foxjava.exe :直接运行程序 xml : Excel文件夹 ##### TemplateXml.xml 根据数据库对应表生成字段描述,生成后最好用WPS打开,然后重新另存为office认识的Excel template : 文件生成模板(非常重要的不能修改) ##### BasePojo.template 所有基础表对象都要继承,方便序列化(系统自动生成) ##### Pager.template 分页对象 (系统自动生成) ##### dao.template 数据库接口Dao(mybatis接口方式,在方法上写sql,复杂的使用sqlProvider) ##### daoSqlProvider.template 复杂sql提供者 ##### service.template 对外开放的接口 ##### serviceImpl.template 实现开放接口,基本数据操作逻辑 /*********************************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值