idea下的spring+Mybatis+聚合工程+逆向工程+简单查询列表

前两天刚上传了聚合工程,现在有时间整理了下与Mybatis和逆向工程的整理,顺带做了一个简单的列表,这里不再叙述搭建工程的步骤

下面是聚合工程的搭建:

https://mp.csdn.net/postedit/81138522

先上传一张工程的结构图,这是普通的包含结构,每个父类包含的子类都一清二楚,也可以创建成平行的结构图,在此就不叙述了

因为使用的是spring+springMVC框架,所以要在web层中导入spring文件+springMVC文件,而且由于项目分层,所以Spring与分层相照应

我们在使用spring+springMVC框架时,配置文件会扫描service包和dao包,这里是直接扫描项目,因为项目的路径不一样,所以就分开写了,还有一个是事物

首先是dao层:

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

   <!-- 数据库连接池 -->
   <!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:conf/db.properties" />
   <!-- 数据库连接池 -->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      destroy-method="close">
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
      <property name="driverClassName" value="${jdbc.driver}" />
      <property name="maxActive" value="10" />
      <property name="minIdle" value="5" />
   </bean>
   <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- 数据库连接池 -->
      <property name="dataSource" ref="dataSource" />
      <!-- 加载mybatis的全局配置文件 -->
      <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
   </bean>
   <!--扫描-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.albb.mapper " />
   </bean>


</beans>

然后是service:

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

   <context:component-scan base-package="com.albb.serviceimpl"/>
</beans>

因为dao层要关联数据库,所以两个差距很大

下面是事物:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
   <!-- 事务管理器 -->
   <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 数据源 -->
      <property name="dataSource" ref="dataSource" />
   </bean>
   <!-- 通知 -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
         <!-- 传播行为 -->
         <tx:method name="save*" propagation="REQUIRED" />
         <tx:method name="insert*" propagation="REQUIRED" />
         <tx:method name="add*" propagation="REQUIRED" />
         <tx:method name="create*" propagation="REQUIRED" />
         <tx:method name="delete*" propagation="REQUIRED" />
         <tx:method name="update*" propagation="REQUIRED" />
         <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
         <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
         <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
      </tx:attributes>
   </tx:advice>
   <!-- 切面 -->
   <aop:config>
      <aop:advisor advice-ref="txAdvice"
         pointcut="execution(* com.albb.serviceimpl.*.*(..))" />
   </aop:config>

</beans>

下面是springMVC层

<?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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

   <context:component-scan base-package="com.albb.controller" />
   <mvc:annotation-driven />
   <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

再看一下其他的结构,还有数据库连接,Mybatis的关联,

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/linux?characterEncoding=utf-8
jdbc.username=****
jdbc.password=****

再然后是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>

</configuration>

啥也没写,不代表不用写,因为我使用就是做了例子,使用的是注解sql,没有使用xml,所以啥也没写

惊喜不惊喜,controller写在web层中,并没有单独的创建出来,其他的也差不多,跟咱们平时一样,只不过,要导的是其他项目的包,

一定要写对地方,写在你创建的的包名下才行

除了我的哪个注接sql是自己写的,其他都是逆向工程的,下面就说下逆向工程,直接上图

直接在该项目下创建子项目《file-open-选中你要打开的项目

然后要打以双窗口的形式打开

 这个就是h支持双窗口

 点击New Window就是打开一个新窗口

这是逆向工程的配置,先来个结构图

下面是工程的配置

<?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>
   <context id="testTables" targetRuntime="MyBatis3">
      <commentGenerator>
         <!-- 是否去除自动生成的注释 true:是 : false:否 -->
         <property name="suppressAllComments" value="true" />
      </commentGenerator>
      <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://localhost:3306/linux" userId="root"
         password="root">
      </jdbcConnection>
      <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
         NUMERIC 类型解析为java.math.BigDecimal -->
      <javaTypeResolver>
         <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>

      <!-- targetProject:生成PO类的位置 -->
      <javaModelGenerator targetPackage="com.albb.pojo"
         targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
         <!-- 从数据库返回的值被清理前后的空格 -->
         <property name="trimStrings" value="true" />
      </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
      <sqlMapGenerator targetPackage="com.albb.mapper"
         targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
      </sqlMapGenerator>
      <!-- targetPackage:mapper接口生成的位置 -->
      <javaClientGenerator type="XMLMAPPER"
         targetPackage="com.albb.mapper"
         targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
      </javaClientGenerator>
      <!-- 指定数据库表 -->
      <table schema="" tableName="product"></table>

   </context>
</generatorConfiguration>

在下面是classpath的配置

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
   <classpathentry kind="src" path="src"/>
   <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
   <classpathentry exported="true" kind="lib" path="lib/log4j-1.2.16.jar"/>
   <classpathentry exported="true" kind="lib" path="lib/mybatis-3.2.3.jar"/>
   <classpathentry exported="true" kind="lib" path="lib/mybatis-generator-core-1.3.2.jar"/>
   <classpathentry exported="true" kind="lib" path="lib/mysql-connector-java-5.1.28-bin.jar"/>
   <classpathentry exported="true" kind="lib" path="lib/ojdbc14.jar"/>
   <classpathentry kind="output" path="bin"/>
</classpath>

projiect的配置

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
   <name>generatorSqlmapCustom</name>
   <comment></comment>
   <projects>
   </projects>
   <buildSpec>
      <buildCommand>
         <name>org.eclipse.jdt.core.javabuilder</name>
         <arguments>
         </arguments>
      </buildCommand>
   </buildSpec>
   <natures>
      <nature>org.eclipse.jdt.core.javanature</nature>
   </natures>
</projectDescription>

日志配置

log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

启动类配置

public void generator() throws Exception{

   List<String> warnings = new ArrayList<String>();
   boolean overwrite = true;
   //指定 逆向工程配置文件
   File configFile = new File("generatorConfig.xml"); 
   ConfigurationParser cp = new ConfigurationParser(warnings);
   Configuration config = cp.parseConfiguration(configFile);
   DefaultShellCallback callback = new DefaultShellCallback(overwrite);
   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
         callback, warnings);
   myBatisGenerator.generate(null);

} 
public static void main(String[] args) throws Exception {
   try {
      GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
      generatorSqlmap.generator();
   } catch (Exception e) {
      e.printStackTrace();
   }
   
}

然后右击运行mian主方法

然后在将生成的dao层,pojo层粘贴到你聚合工程中,然后就可以运行了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值