spring+mybatis项目的搭建

我用的eclipse直接生成的springMVC项目,自动生成了root-context.xml和servlet-context.xml这两个文件

首先来看servlet-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/assets/**" location="/WEB-INF/assets/" />
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	<context:component-scan base-package="com.ldl.customize.controller" />

</beans:beans>
<resources mapping="/assets/**" location="/WEB-INF/assets/" />
访问静态资源用的


注解之类的都是自动扫描,免去了一个个的配置


root-context.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:aop="http://www.springframework.org/schema/aop"
	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
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- Root Context: defines shared resources visible to all other web components -->

	<bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
        <property name="statementExecutableSqlLogEnable" value="true" />
  	</bean>
	<!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://192.168.200.64:3306/customize" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <!-- 配置初始化大小、最小、最大 -->
	    <property name="initialSize" value="3" />
	    <property name="minIdle" value="1" />
	    <property name="maxActive" value="50" />

	    <!-- 配置获取连接等待超时的时间 -->
	    <property name="maxWait" value="60000" />

	    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
	    <property name="timeBetweenEvictionRunsMillis" value="60000" />

	    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
	    <property name="minEvictableIdleTimeMillis" value="300000" />

	    <property name="validationQuery" value="SELECT 'x'" />
	    <property name="testWhileIdle" value="true" />
	    <property name="testOnBorrow" value="false" />
	    <property name="testOnReturn" value="false" />

	    <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
	    <!-- <property name="poolPreparedStatements" value="true" />
	    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->

	    <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
	    <property name="filters" value="stat" />
	    <!-- 配置输出日志输出可执行的SQL -->
	    <property name="proxyFilters">
        <list>
            <ref bean="log-filter"/>
        </list>
    </property>
    </bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <!-- MyBatis 的 XML 配置文件路径 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
        <!-- 扫描自动生成的xml文件 --><!-- Mybatis XML映射文件 -->

        <property name="mapperLocations"  >
            <list><!-- Mybatis XML映射文件 -->
                <value>classpath*:com/ldl/customize/mapper/*.xml</value>
                <!-- 扫描自己写的xml文件-->
                <!-- <value>classpath*:com/weshare/*/api/xml/*.xml</value> -->
            </list>
        </property>

    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

	<!-- 扫描mybatisGenerator 自动生成的  所有接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.ldl.customize.dao" ></property>
    </bean>

    <!-- 数据库事务策略-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        <!--
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="ins*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="select*" read-only="true" />
             -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
</beans>



mybatis的xml文件都是自动扫描,免得配置麻烦,连接池使用了druid,还有监控功能,感觉比较方便



sqlMapConfig.xml  mybatis的配置文件


<?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="false" />
        <!-- 查询时,关闭关联对象即时加载以提高性能 -->
        <setting name="lazyLoadingEnabled" value="false" />
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指定),不会加载关联表的所有字段,以提高性能 -->
        <setting name="aggressiveLazyLoading" value="false" />
        <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
        <setting name="multipleResultSetsEnabled" value="true" />
        <!-- 允许使用列标签代替列名 -->
        <setting name="useColumnLabel" value="true" />
      	<!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
        <!-- <setting name="useGeneratedKeys" value="true" /> -->
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
        <!-- <setting name="autoMappingBehavior" value="FULL" /> -->
        <!-- 对于批量更新操作缓存SQL以提高性能  -->
        <setting name="defaultExecutorType" value="BATCH" />
        <!-- 数据库超过25000秒仍未响应则超时 -->
        <!-- <setting name="defaultStatementTimeout" value="25000" /> -->
    </settings>
</configuration>




generatorConfig.xml  mybatis自动生成代码的配置


<?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="D:\develop\apache-maven-3.2.1\repo\m2\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar" />

	<context id="MysqlTables" targetRuntime="MyBatis3">

	<commentGenerator>
		<property name="suppressAllComments" value="true" />
	</commentGenerator>

	<jdbcConnection driverClass="com.mysql.jdbc.Driver"
		connectionURL="jdbc:mysql://192.168.200.64:3306/customize" userId="root" password="root">
	</jdbcConnection>

	<javaTypeResolver>
		<property name="forceBigDecimals" value="false" />
	</javaTypeResolver>

	<javaModelGenerator
		targetPackage="com.ldl.customize.entity"
		targetProject="src\main\java">
		<property name="enableSubPackages" value="true" />
		<property name="trimStrings" value="true" />
	</javaModelGenerator>

	<sqlMapGenerator
		targetPackage="com.ldl.customize.mapper"
		targetProject="src\main\resources">
		<property name="enableSubPackages" value="false" />
	</sqlMapGenerator>

	<javaClientGenerator
		type="XMLMAPPER"
		targetPackage="com.ldl.customize.dao"
		targetProject="src\main\java">
		<property name="enableSubPackages" value="true" />
		<property name="exampleMethodVisibility" value="public" />
	</javaClientGenerator>

	<table tableName="user" domainObjectName="User">
		<property name="useActualColumnNames"   value="false"/>
	</table>

	<table tableName="login_info" domainObjectName="LoginInfo">
		<property name="useActualColumnNames"   value="false"/>
	</table>

	</context>
</generatorConfiguration>



这个使用了maven插件



<plugin>
                <groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
                <configuration>
                 	<verbose>true</verbose>
                 	<overwrite>true</overwrite>
                </configuration>
            </plugin>



mvn mybatis-generator:generate 这样就可以执行了或者在eclipse里配置一下


spring+mybatis项目的搭建_第1张图片



你可能感兴趣的:(spring+mybatis项目的搭建)