[关闭]
@Pollux 2016-07-23T03:21:42.000000Z 字数 6281 阅读 821

先整合spring和mybatis

web


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <generatorConfiguration>
  4. <!-- 数据库驱动包位置 -->
  5. <!-- <classPathEntry location="C:\Program Files (x86)\MySQL\mysql-connector-java-5.1.36-bin.jar" /> -->
  6. <classPathEntry location="E:\generator\ojdbc14-10.2.0.4.0.jar" />
  7. <context id="DB2Tables" targetRuntime="MyBatis3">
  8. <commentGenerator>
  9. <property name="suppressAllComments" value="true" />
  10. </commentGenerator>
  11. <!-- 数据库链接URL、用户名、密码 -->
  12. <!--
  13. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root">
  14. -->
  15. <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@172.17.1.111:1521:ORCL" userId="xt" password="xtGZHU34">
  16. </jdbcConnection>
  17. <javaTypeResolver>
  18. <property name="forceBigDecimals" value="false" />
  19. </javaTypeResolver>
  20. <!-- 生成模型的包名和位置 -->
  21. <javaModelGenerator targetPackage="com.pollux.pojo"
  22. targetProject="E:\generator\generator\src">
  23. <property name="enableSubPackages" value="true" />
  24. <property name="trimStrings" value="true" />
  25. </javaModelGenerator>
  26. <!-- 生成的映射文件包名和位置 -->
  27. <sqlMapGenerator targetPackage="com.pollux.mapper" targetProject="E:\generator\generator\src">
  28. <property name="enableSubPackages" value="true" />
  29. </sqlMapGenerator>
  30. <!-- 生成DAO的包名和位置 -->
  31. <javaClientGenerator type="XMLMAPPER" targetPackage="com.pollux.dao" targetProject="E:\generator\generator\src">
  32. <property name="enableSubPackages" value="true" />
  33. </javaClientGenerator>
  34. <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
  35. <table tableName="USERS" domainObjectName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  36. </context>
  37. </generatorConfiguration>
  1. //修改包名之后在命令行中执行生成语句
  2. java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
  3. //成功之后把生成的三个文件拖进项目中
  1. <!--放在resources文件夹中-->
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  8. <context:component-scan base-package="com.pollux.service">
  9. </context:component-scan>
  10. </beans>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
  8. <!-- 1. 数据源 : DriverManagerDataSource -->
  9. <!-- <bean name="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
  10. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  11. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
  12. <property name="username" value="root"></property>
  13. <property name="password" value="root"></property>
  14. </bean> -->
  15. <bean name="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
  16. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
  17. <property name="url" value="jdbc:oracle:thin:@172.17.1.111:1521:ORCL"></property>
  18. <property name="username" value="xt"></property>
  19. <property name="password" value="xtGZHU34"></property>
  20. </bean>
  21. <!-- 2. mybatis 的SqlSession 的工厂: SqlSessionFactoryBean -->
  22. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  23. <property name="dataSource" ref="dataSource"></property>
  24. <property name="mapperLocations" value="classpath:com/pollux/mapper/*.xml"></property>
  25. </bean>
  26. <!-- 3. mybatis 自动扫描加载Sql 映射文件 : MapperScannerConfigurer -->
  27. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  28. <property name="basePackage" value="com.pollux.dao" />
  29. <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  30. </bean>
  31. <!-- 4. 事务管理 : DataSourceTransactionManager -->
  32. <bean id="txManager"
  33. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  34. <property name="dataSource" ref="dataSource" />
  35. </bean>
  36. <!-- 5. 使用声明式事务 -->
  37. <tx:annotation-driven transaction-manager="txManager" />
  38. </beans>
  1. package com.pollux;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.pollux.pojo.user;
  6. import com.pollux.service.userservice;
  7. public class test {
  8. private userservice userservice;
  9. @Test
  10. public void test(){
  11. ApplicationContext applicationContext;
  12. applicationContext = new ClassPathXmlApplicationContext(
  13. new String[]{"spring/spring.xml","spring/spring-mybatis.xml"}
  14. );
  15. userservice = (userservice) applicationContext.getBean("userservice");
  16. // user user = new user();
  17. // user.setName("yxin");
  18. // user.setPassword("456");
  19. // user result = userservice.login(user);
  20. user result = userservice.getfbb("1F8EB364DE194BCFB22FB5D502284971");
  21. System.out.println(result.toString());
  22. }
  23. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注