我是MyBatis的初学者.
我只想知道如何从类的实例插入一组对象.假设我有一个与一对多关系中的注释相关的类用户.我只想提一下,我通过Hibernate的hbm2ddl使用JPA 2注释构建了我的模式.我将在下面的示例代码中添加我使用的关键JPA注释.
这是一个示例:
@Entity
public class User {
...
@OneToMany
@JoinColumn(name="user")
public List<Note> getNotes() {...}
...
}
Run Code Online (Sandbox Code Playgroud)
现在,每次我在User表中插入一些内容时,如果列表不为空,我必须将实例插入到Note表中.记下Note表中的@JoinColumn,它应该具有已插入User的id,我已将其设置为自动生成.
有人有这样的工作吗?谢谢.
我有POJO课程:
class Ticket {
private int id;
private double cost;
private Date time;
private List<Place> places;
// Getters and setters here
}
class Place {
private int row;
private int place;
// Getters and setters here
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一张票和一些地方:
Ticket ticket = new Ticket();
ticket.setCost(58.7);
ticket.setTime(new Date());
Place place1 = new Place();
place1.setRow(1);
place1.setPlace(2);
ticket.addPlace(place1);
Place place2 = new Place();
place2.setRow(3);
place2.setPlace(4);
ticket.addPlace(place2);
Run Code Online (Sandbox Code Playgroud)
现在我想将它保存到DB:
session.insert("insertTicket", ticket);
session.commit();
Run Code Online (Sandbox Code Playgroud)
在MapperConfig.xml中,我写这行:
<insert id="insertTicket" parameterType="Ticket">
INSERT INTO tickets (cost, time) VALUES (#{cost}, #{time})
</insert>
Run Code Online (Sandbox Code Playgroud)
如何在自动模式下保存 …
在mybatis中使用注释,我们可以将返回类型作为普通地图吗?
基本上,我想要这样的东西
@Select("select a, b from tableA")
public Map<String, String> getItems();
Run Code Online (Sandbox Code Playgroud)
哪里
mysql> select * from tableA;
+------+------+
| a | b |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
+------+------+
mysql> desc tableA;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | varchar(10) | YES | | NULL | |
| b | varchar(10) | YES | | NULL | | …Run Code Online (Sandbox Code Playgroud) 我试图传中startSequenceId,stopSequenceId,orderNumber到SQL映射,但是我不希望用一个类型的对象,也就是说parameterType="com.abc.Order",我可以这样做?
<select id="getSequenceIdByOrderNumber" parameterType="?" resultType="int">
select *
from log
where seq_id
between #{startSequenceId} and #{stopSequenceId}
and order_no = #{orderNumber}
and rownum = 1
</select>
Run Code Online (Sandbox Code Playgroud) 我想在MyBatis中为Select语句使用String参数.我的mapper.xml:
<select id="selectAll" parameterType="String" resultMap="fastXMLResultMap">
SELECT CREATIONDATE, DOCUMENTID, TITEL, REGTITEL, INFORCEDATE, DOCTYPE
FROM #{databBaseTable}
</select>
Run Code Online (Sandbox Code Playgroud)
和调用功能:
public List<FastXMLObject> selectAll(String databBaseTable) {
SqlSession session = sqlSessionFactory.openSession();
System.out.println("Table: "+databBaseTable);
try {
List<FastXMLObject> list = session.selectList("FastXMLObject.selectAll",databBaseTable);
return list;
} finally {
session.close();
}
}
Run Code Online (Sandbox Code Playgroud)
字符串dataBaseTable是我的数据库的表的名称(谁会想到)因为我想从verious表中动态获取数据.
但不幸的是,这不起作用:错误:ORA-00903:UngültigerTabellenname(表名无效)但它不是.当我打印出"databBaseTable"的值时,它就是表的确切名称.当我将表的名称写入我的mapper.xml而没有变量时,它可以工作.我做错了什么?
我已经谷歌搜索了一段时间,似乎无法找到任何真正的答案.
我有一个Oracle存储过程,它有许多in参数,其类型是表rowtype的表.例如:
在pacakge宣布:
TYPE param1_type_t IS TABLE OF table1%ROWTYPE;
TYPE param2_type_t IS TABLE OF table2%ROWTYPE;
TYPE param3_type_t IS TABLE OF table3%ROWTYPE;
Run Code Online (Sandbox Code Playgroud)
Oracle程序:
PROCEDURE my_proc
(
parameter1 IN param1_type_t,
parameter2 IN param2_type_t,
parameter3 IN param3_type_t
)
Run Code Online (Sandbox Code Playgroud)
在java方面,我有3个对应的对象列表,表示用Java填充的每个参数.在这种情况下,是否可以使用MyBatis调用Oracle过程?
<update id="callOracleSP" statementType="CALLABLE">
{CALL my_proc( #{param1, mode=IN},
#{param2, mode=IN},
#{param3, mode=IN}
)
}
</update>
Run Code Online (Sandbox Code Playgroud)
对象本身是具有String和Integer属性的简单VO以及它们各自的getter和setter.
我不确定如何继续.我是否需要以某种方式将Java对象列表映射到Oracle类型?
我有一个(Java)类,有许多实例字段(其中许多是可选的).我希望所有字段(因此类)都是不可变的.所以,我想使用Builder Pattern来构造类的实例.
我可以使用Builder模式配置myBatis来创建类的实例吗?我知道我可以让myBatis返回一张地图并使用该地图在我的代码中构建实例.但是,我正在寻找一种方法来配置这种映射(或使用一些约定),类似于如何通过使用Java Bean和构造函数来创建实例.
编辑(包括一个例子)
这是一个例子:
package com.example.model;
// domain model class with builder
public final class CarFacts {
private final double price;
private final double numDoors;
private final String make;
private final String model;
private final String previousOwner;
private final String description;
public static class Builder {
// required params
private final double price;
private final String make;
private final String model;
// optional params
private final String previousOwner;
private final String description;
private final double numDoors;
public Builder(double …Run Code Online (Sandbox Code Playgroud) 我使用的是Spring 3.1.1,MyBatis 3.1.1,MySQL 5.0.67.我的Spring配置如下:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="validationQuery" value="select 1"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="14400000"/>
<property name="testOnBorrow" value="false"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/myBatisConfig.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
Run Code Online (Sandbox Code Playgroud)
log4.properties如下:
log4j.logger.org.springframework=DEBUG
log4j.logger.org.apache=DEBUG
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
Run Code Online (Sandbox Code Playgroud)
通过这些配置,我可以看到执行的SQL查询语句和该查询的参数但我看不到查询结果日志.我的日志是这样的:
[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
[org.mybatis.spring.SqlSessionUtils] - SqlSession …Run Code Online (Sandbox Code Playgroud) 我applicationContext.xml在classpath的根目录下有下一个文件:
<context:annotation-config />
<context:property-placeholder location="classpath:props/datasource.properties" />
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:url="${jdbc.url}"
p:driverClassName="${jdbc.driverclass}"
p:validationQuery="SELECT sysdate FROM dual" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="datasource"
p:mapperLocations="classpath:mappers/*-mapper.xml" />
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="datasource" />
<bean id="mappeScannerConfigurere" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactory-ref="sqlSessionFactory"
p:basePackage="com.mypackage" />
Run Code Online (Sandbox Code Playgroud)
props/datasource.properties 也存在于classpath的根目录中,内容如下:
jdbc.url=myjdbcurl
jdbc.driverclass=myClass
jdbc.username=myUserName
jdbc.password=myPassword
Run Code Online (Sandbox Code Playgroud)
我有一个spring托管测试,我声明通过下一个注释使用前面提到的applicationContext.xml:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
Run Code Online (Sandbox Code Playgroud)
当我调用测试方法时,我从spring获得下一个错误:
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driverclass}'
Run Code Online (Sandbox Code Playgroud)
据我所知,sping没有解析对jdbc.driverclass的引用.我做错了什么?
PS:我正在使用spring 3.2.3.RELEASE
**
**
也许问题可能在于MapperScannerConfigurer.这是一个BeanDefinitionRegistryPostProcessor和Javadoc说:
扩展到标准BeanFactoryPostProcessor SPI,允许在常规BeanFactoryPostProcessor检测开始之前注册其他bean定义
因此,MapperScannerConfigurer通过sqlSessionFactory实例化数据源对象BeanFacoryPostProcessor …
我使用Mybatis(3.2.7版本)作为我的JAVA项目的ORM框架.由于我来自JPA背景,我很想探索Mybatis支持的LAZYLOADING.但我无法透露任何实质性内容.
(我正在使用JAVA API和注释配置MYBATIS仅用于查询目的)
根据Mybatis文档: 1.lazyLoadingEnabled:默认值= TRUE
全局启用或禁用延迟加载.启用后,所有关系都将被延迟加载.通过在其上使用fetchType属性,可以为特定关系取代此值.
2. aggressiveLazyLoading:默认值= TRUE
启用后,将在调用任何延迟属性时完全加载具有延迟加载属性的对象.否则,每个属性都按需加载.
使用以下属性,我尝试了以下代码:
一个.JAVA课程:
Feedback.java
public class Feedback implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String message;
/**
* while loading Feedback, I want sender object to be lazily loaded
*/
private User sender;
private boolean seen;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
User.java
public class User implements Serializable, {
private static final long serialVersionUID = 1L;
private int id;
private …Run Code Online (Sandbox Code Playgroud)