mybatis根据oracle数据库生成的代码怎么加注释

mybatis根据oracle数据库生成的代码怎么加注释,第1张

1、首先在电脑上打开sql数据库软件。然后右键点击新建查询。

2、然后用create database命令创建一个数据库。命令为create database 酒店管理。

3、然后创建数据库的主文件。on primary(name =酒店管理,)。

4、设置文件的保存位置和名字。命令为filename = 'E:\酒店管理.mdf'。

5、创建数据库的日志文件。命令为log on(name =酒店管理log,)。

6、再设置日志文件的储存位置。命令为filename='E:\酒店管理.ldf'。最后运行sql语句就可以得到一个酒店管理的数据库。

MyBatis的注解方式就是将SQL语句直接写在接口上。在MyBatis注解SQL中,最基本的就是@Select、@Insert、@Update和@Delete四种。

删除相关的SQL写在@Delete注解中,花括号里面的内容可以是字符串也可以是字符串数组。

修改相关的SQL写在@Update注解中,花括号里面的内容可以是字符串也可以是字符串数组。

首先创建UserMapper接口,定义接口方法。然后创建UserProvider类,在UserProvider类中定义与接口中方法对应的返回SQL语句的方法。

Provider的注解中提供了两个必填的属性 type 和 method 。 type 配置的是一个包含 method 属性指定方法的类,这个类必须有空的构造方法,这个方法的值就是要执行的SQL语句,并且 method 属性指定的方法的返回值必须是String类型。

mybatis和spring的整合步骤:

1)使用mybatis,必须有个全局配置文件configuration.xml,来配置mybatis的缓存,延迟加载等等一系列属性,该配置文件示例如下:

Java代码

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

<configuration>

<settings>

<!-- 全局映射器启用缓存 -->

<setting name="cacheEnabled" value="true" />

<!-- 查询时,关闭关联对象即时加载以提高性能 -->

<setting name="lazyLoadingEnabled" value="true" />

<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由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>

<!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径 -->

<typeAliases>

<typeAlias alias="TestBean"

type="com.wotao.taotao.persist.test.dataobject.TestBean" />

</typeAliases>

<!-- 非注解的sql映射文件配置,如果使用mybatis注解,该mapper无需配置,但是如果mybatis注解中包含@resultMap注解,则mapper必须配置,给resultMap注解使用 -->

<mappers>

<mapper resource="persist/test/orm/test.xml" />

</mappers>

</configuration>

2)该文件放在资源文件的任意classpath目录下,假设这里就直接放在资源根目录,等会spring需要引用该文件。

查看ibatis-3-config.dtd发现除了settings和typeAliases还有其他众多元素,比如properties,objectFactory,environments等等,这些元素基本上都包含着一些环境配置,数据源定义,数据库事务等等,在单独使用mybatis的时候非常重要,比如通过以构造参数的形式去实例化一个sqlsessionFactory,就像这样:

Java代码

SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader)

SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, properties)

SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, environment, properties)

而typeHandlers则用来自定义映射规则,如可以自定义将Character映射为varchar,plugins元素则放了一些拦截器接口。

2)在spring配置文件中指定c3p0数据源定义如下:

Java代码

<!-- c3p0 connection pool configuration -->

<bean id="testDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<!-- 数据库驱动 -->

<property name="driverClass" value="${db.driver.class}" />

<!-- 连接URL串 -->

<property name="jdbcUrl" value="${db.url}" />

<!-- 连接用户名 -->

<property name="user" value="${db.username}" />

<!-- 连接密码 -->

<property name="password" value="${db.password}" />

<!-- 初始化连接池时连接数量为5个 -->

<property name="initialPoolSize" value="5" />

<!-- 允许最小连接数量为5个 -->

<property name="minPoolSize" value="5" />

<!-- 允许最大连接数量为20个 -->

<property name="maxPoolSize" value="20" />

<!-- 允许连接池最大生成100个PreparedStatement对象 -->

<property name="maxStatements" value="100" />

<!-- 连接有效时间,连接超过3600秒未使用,则该连接丢弃 -->

<property name="maxIdleTime" value="3600" />

<!-- 连接用完时,一次产生的新连接步进值为2 -->

<property name="acquireIncrement" value="2" />

<!-- 获取连接失败后再尝试10次,再失败则返回DAOException异常 -->

<property name="acquireRetryAttempts" value="10" />

<!-- 获取下一次连接时最短间隔600毫秒,有助于提高性能 -->

<property name="acquireRetryDelay" value="600" />

<!-- 检查连接的有效性,此处小弟不是很懂什么意思 -->

<property name="testConnectionOnCheckin" value="true" />

<!-- 每个1200秒检查连接对象状态 -->

<property name="idleConnectionTestPeriod" value="1200" />

<!-- 获取新连接的超时时间为10000毫秒 -->

<property name="checkoutTimeout" value="10000" />

</bean>

配置中的${}都是占位符,在指定数据库驱动打war时会自动替换,替换的值在父pom中配置。

3)需要一个sessionFactory来生成session,sessionFactory配置如下:

Java代码

<bean id="testSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="classpath:configuration.xml" />

<property name="dataSource" ref="testDataSource" />

</bean>

4)配置一个映射器接口来对应sqlSessionTemplate,该映射器接口定义了接口方法:

Java代码

<!-- data OR mapping interface -->

<bean id="testMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

<property name="sqlSessionFactory" ref="testSqlSessionFactory" />

<property name="mapperInterface" value="com.wotao.taotao.persist.test.mapper.TestMapper" />

</bean>

5)至此,一个完整的myabtis整合spring的配置文件看起来应该如下所示:

Java代码

<?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:context="http://www.springframework.org/schema/context"

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-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- c3p0 connection pool configuration -->

<bean id="testDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="${db.driver.class}" />

<property name="jdbcUrl" value="${db.url}" />

<property name="user" value="${db.username}" />

<property name="password" value="${db.password}" />

<property name="initialPoolSize" value="5" />

<property name="minPoolSize" value="5" />

<property name="maxPoolSize" value="20" />

<property name="maxStatements" value="100" />

<property name="maxIdleTime" value="3600" />

<property name="acquireIncrement" value="2" />

<property name="acquireRetryAttempts" value="10" />

<property name="acquireRetryDelay" value="600" />

<property name="testConnectionOnCheckin" value="true" />

<property name="idleConnectionTestPeriod" value="1200" />

<property name="checkoutTimeout" value="10000" />

</bean>

<bean id="testSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="classpath:configuration.xml" />

<property name="dataSource" ref="testDataSource" />

</bean>

<!-- data OR mapping interface -->

<bean id="testMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

<property name="sqlSessionFactory" ref="testSqlSessionFactory" />

<property name="mapperInterface" value="com.wotao.taotao.persist.test.mapper.TestMapper" />

</bean>

<!-- add your own Mapper here -->

<!-- comment here, using annotation -->

<!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">-->

<!-- <constructor-arg index="0" ref="sqlSessionFactory" />-->

<!-- </bean>-->

<!-- base DAO class, for module business, extend this class in DAO -->

<!-- <bean id="testBaseDAO" class="com.test.dao.TestBaseDAO">-->

<!-- <property name="sqlSessionTemplate" ref="sqlSessionTemplate" />-->

<!-- </bean>-->

<!-- <bean id="testDAO" class="com.test.dao.impl.TestDAOImpl" />-->

<!-- you can DI Bean if you don't like use annotation -->

</beans>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11713359.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存