搭建maven+spring+freemaker+mybatis环境之四

来源:互联网 发布:网络电视机顶盒牌子 编辑:程序博客网 时间:2024/06/13 21:40

此篇在前面的基础上加入了mybatis

先说两个配置文件的修改,在spring-servlet.xml里加入了下面的代码,抽出了freemaker的配置

<span style="white-space:pre"></span><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations" value="classpath:freemarker.properties" /></bean>

在spring.xml里加入了 下面的代码,用于存放数据库连接的属性

<context:property-placeholder location="classpath:jdbc.properties" />
jdbc.properties的内容:
<pre name="code" class="html">driver=com.mysql.jdbc.Driverurl=jdbc:mysql://121.40.120.111:3306/gnxusername=gnxMonitor1password=gnxPassword


一、pom.xml里增加数据库连接需要的包,我挨个搜了搜,尽量保证最新,并把之前spring的相关包升级到了,<version>4.2.2.RELEASE</version>

<span style="white-space:pre"></span><!-- mybatis 的jar --><dependency>              <groupId>org.mybatis</groupId>              <artifactId>mybatis</artifactId>              <version>3.3.0</version>          </dependency>          <dependency>              <groupId>org.mybatis</groupId>              <artifactId>mybatis-spring</artifactId>              <version>1.2.3</version>          </dependency>                  <!-- 数据库连接dbcp --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId><version>2.1.1</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><!-- mysql的jar --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.35</version></dependency>

二.所有的配置文件都是放在src/main/resources下面,增加了spring-mybatis.xml,并在web.xml里做了初始化


spring-mybatis.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="      http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-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      http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd      http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd      "><!--创建jdbc数据源 dbcp --><!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> --><bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><!-- data source configuration --><property name="initialSize" value="10" /><!-- initial connections --><property name="maxTotal" value="100" /><!-- MAX connections --><property name="maxIdle" value="50" /><!-- MAX idle connections --><property name="minIdle" value="20" /><!-- MIN idle connections --><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="validationQuery" value="select 1" /><property name="timeBetweenEvictionRunsMillis" value="20000" /><property name="numTestsPerEvictionRun" value="100" /></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描entity目录 xml文件 --><property name="mapperLocations" value="classpath:com/template/persist/mapper/*.xml"></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 自动扫描 mybatis的接口 --><property name="basePackage" value="com.template.persist.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- 事务 --><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 -->  <tx:annotation-driven transaction-manager="txManager" /> </beans>
启动没问题
明天写测试类......先.睡觉

0 0