spring自动建表 很蛋疼的问题(exception)create update

 <?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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

         <context:component-scan base-package="org.itec.elwg" />
         <bean id="propertyConfiger" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
             <property name="location">
                 <value>classpath:configuration.properties</value>
             </property>
         </bean>
         <!-- 配置数据源 -->
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
             <property name="driverClassName" value="${jdbc.driverClassName}" />
             <property name="url" value="${jdbc.url}" />
             <property name="username" value="${jdbc.username}" />
             <property name="password" value="${jdbc.password}" />
</bean>

<!-- 配置数据源事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
             <property name="dataSource">
                 <ref bean="dataSource" />
            </property>
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
         </bean>
         <!-- 配置sessionFactory -->
         <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
             <property name="dataSource">
                 <ref bean="dataSource" />
             </property>
             <property name="hibernateProperties">
                 <props>
          <prop key="hibernate.hbm2ddl.auto">create</prop>             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                     <prop key="hibernate.show_sql">true</prop>
                     <prop key="hibernate.format_sql">true</prop>
                 </props>
             </property>
             <!-- 实体类 -->
             <property name="packagesToScan">
                 <list>
                     <value>org.my.entity</value>
                 </list>
             </property>
          </bean>

         <!-- 配置HibernateTemplate -->
         <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
             <property name="sessionFactory">
                 <ref bean="sessionFactory" />
             </property>
         </bean>
   
    <!-- 事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
   
    <!-- Spring AOP config -->
    <aop:config>
        <!-- 切入点 -->
        <aop:pointcut id="servicePointcut" expression="execution(* org.my.service.impl.*.*(..))" /> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
    </aop:config>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MyBatis中执行建表命令,可以通过两种方式进行操作:使用MyBatis的注解或者使用MyBatis的XML映射文件。 1. 使用注解方式: - 在实体类上使用`@Table`注解指定表名和字段信息。例如: ```java @Table(name = "my_table") public class MyEntity { @Id private Long id; private String name; // ... } ``` - 创建一个接口,并使用`@Mapper`注解标记为MyBatis的Mapper接口。在接口中定义建表语句的SQL语句。例如: ```java @Mapper public interface MyTableMapper { @Update("CREATE TABLE my_table (id BIGINT PRIMARY KEY, name VARCHAR(255))") void createTable(); } ``` - 在Spring Boot应用程序中,注入`MyTableMapper`接口,并在适当的时机调用`createTable()`方法来执行建表语句。例如,在应用程序启动时执行建表语句: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyTableMapper myTableMapper; public MyApplication(MyTableMapper myTableMapper) { this.myTableMapper = myTableMapper; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myTableMapper.createTable(); } } ``` 2. 使用XML映射文件方式: - 创建一个XML映射文件,定义建表语句的SQL语句。例如,在`my_table.xml`中定义建表语句: ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.MyTableMapper"> <update id="createTable"> CREATE TABLE my_table (id BIGINT PRIMARY KEY, name VARCHAR(255)) </update> </mapper> ``` - 创建一个接口,并使用`@Mapper`注解标记为MyBatis的Mapper接口。在接口中定义一个方法,通过`@Update`注解引用XML映射文件中的建表语句。例如: ```java @Mapper public interface MyTableMapper { @UpdateProvider(type = MyTableSqlProvider.class, method = "createTable") void createTable(); } ``` - 创建一个SQL提供者类,用于读取XML映射文件中的SQL语句。例如,在`MyTableSqlProvider`中定义一个方法来读取建表语句: ```java public class MyTableSqlProvider { public String createTable() { try { InputStream inputStream = Resources.getResourceAsStream("my_table.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream); Configuration configuration = factory.getConfiguration(); MappedStatement statement = configuration.getMappedStatement("com.example.mapper.MyTableMapper.createTable"); BoundSql boundSql = statement.getBoundSql(null); return boundSql.getSql(); } catch (IOException e) { throw new RuntimeException("Failed to read SQL from XML file", e); } } } ``` - 在Spring Boot应用程序中,注入`MyTableMapper`接口,并在适当的时机调用`createTable()`方法来执行建表语句。例如,在应用程序启动时执行建表语句: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyTableMapper myTableMapper; public MyApplication(MyTableMapper myTableMapper) { this.myTableMapper = myTableMapper; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myTableMapper.createTable(); } } ``` 以上是两种使用MyBatis执行建表命令的常用方式。你可以根据自己的项目需求选择其中一种方式进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值