spring+mybatis項目的搭建

我用的eclipse直接生成的springMVC項目,自動生成了root-context.xml和servlet-context.xml這兩個文件 java

首先來看servlet-context.xml mysql


<?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/" />
訪問靜態資源用的


註解之類的都是自動掃描,免去了一個個的配置 web


root-context.xml spring


<?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的配置文件 sql


<?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裏配置一下


相關文章
相關標籤/搜索