是否有带注释的iBATIS 3 的综合示例或教程?
我特别感兴趣的是从基于XML的映射器配置转向使用纯Java注释,其中SQL语句通常采用或返回复杂的数据结构.
此外,在我看到的样本中,当从基于XML的映射器转移到基于注释的映射器接口时,配置文件是如何被调整的还不清楚.
我使用了mybatis-spring-1.0.3-SNAPSHOT mybatis-3.0.6 spring3.0.6.我试图从这样的表中删除记录:
<delete id="deleteNote" parameterType="hashMap">
DELETE FROM BBSCS_NOTE
<where>
<if test="ids !=null and ids.length > 0">
<foreach collection="ids" item="id" open="(" close=")" separator=",">
ID IN #{id}
</foreach>
</if>
<if test="toID != null and toID != ''">AND TOID = #{toID}</if>
<if test="fromID != null and fromID != ''">AND FROMID = #{fromID}</if>
<if test="noteType != -1">AND NOTETYPE = #{noteType}</if>
</where>
</delete>
Run Code Online (Sandbox Code Playgroud)
如你所见,它是一个动态的sql.像这样的java测试代码:
Map map = new HashMap();
String ids[] = {"1","2","3"};
map.put("ids", ids);
noteService.del(map);
Run Code Online (Sandbox Code Playgroud)
当我执行java测试代码时,有一些像这样的异常:
org.springframework.jdbc.UncategorizedSQLException: Error setting null parameter. Most JDBC …Run Code Online (Sandbox Code Playgroud) 我使用java.time.LocalDate(Java 8)来表示Java类中的一些成员字段.
class Test{
private LocalDate startDate;
private LocalDate endDate;
//other fields
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我也在使用mybatis来与我的数据库进行交互.
在从DB检索某些数据时,所有其他字段都会正确填充,但startDate和endDate字段最终为null.但是,如果我使用java.util.Date,就像在
private Date startDate;
private Date endDate;
Run Code Online (Sandbox Code Playgroud)
当我将它们声明为java.util.Date时,我得到在这两个字段(startDate和endDate)中检索到的正确值.
是因为mybatis目前没有将'Timestamp'(SQL Server)映射到java.time吗?
我应该如何使用java.time.LocalDate与MyBatis进行映射?
我喜欢XML表示法来指定连接字符串等全局参数.我也喜欢Mapper注释.当我尝试将两者结合起来时,我得到了这个例外.
有没有办法将两者结合起来?我想使用XML文件进行全局配置,但mybatis会考虑使用Mapper接口.
问题是SqlSessionFactoryBuilder().build()需要一个Reader(我想用它来传递XML配置),或者一个Configuration对象(我看到它有addMappers()可以帮助我的方法) - 但我不知道了解如何将两者结合起来.
我正在使用MyBatis 3.0.3并且有问题:数据库中的某些列具有带下划线的名称,这些列应该映射到实体属性(当然是在camelCase中)
class User {
private String first_name;
...
}
public interface UserDao {
@Select("SELECT * FROM users")
List<User> findAllUsers();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我看不出任何解决方法(就像在JPA中完成的那样 - @Column(name ="first_name")).我可以在select-clause中为这些列创建别名(sush作为firstName作为firstName等),但这看起来也很蹩脚.
有任何想法吗?谢谢.
我对春天的mybatis有很多问题.这是我的spring配置:我有相同的配置,ComuneMapper.java和ComuneMapper.xml保留在同一个文件夹中.但我有这个错误有人帮我meeeee
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan
base-package="com.aieap" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Configurazione Spring MVC View Resolver -->
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property
name="prefix"
value="/jsp/" />
<property
name="suffix"
value=".jsp" />
</bean>
<!-- Flow Handler Adapter --> …Run Code Online (Sandbox Code Playgroud) 我已经用enum打了一段时间,但它不会按我的方式行事.有没有人可以给我提示?我试图在MySql中使用Enum类型,我也在我的代码中使用Enum类.
由于代码现在它将插入星期一,但它也会尝试在workdayID上插入MONDAY ...我没有得到workdayID.我相信我必须以某种方式处理DAY_TYPE ...定义一个typeHandler也许?但我试过了,它不会起作用,或者因为我无法正确做到这一点?
我也试过了org.apache.ibatis.type.EnumTypeHandler,但没有成功,就像这样
#{DAY_TYPE,typeHandler=org.apache.ibatis.type.EnumTypeHandler}
Run Code Online (Sandbox Code Playgroud)
DAY_TYPE.java
package tut.model;
import java.io.Serializable;
public enum DAY_TYPE implements Serializable{
MONDAY(1),TUESDAY(2),WEDNESDAY(3),THURSDAY(4),FRIDAY(5),SATURDAY(6),SUNDAY(7);
private int id; private int workdayID;
private DAY_TYPE(int id) { this.id = id; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getWorkdayID() { return workdayID; }
public void setWorkdayID(int workdayID) {this.workdayID = workdayID;}
}
Run Code Online (Sandbox Code Playgroud)
DAY_TYPE_Mapper.xml
<insert id="insert" parameterType="DAY_TYPE" useGeneratedKeys="true" keyProperty="iddaytaype">
INSERT INTO daytaype (DAY_TYPE, workdayID)
VALUES (#{DAY_TYPE},#{workdayID})
<selectKey keyProperty="iddaytaype" resultType="long" order="AFTER">
SELECT …Run Code Online (Sandbox Code Playgroud) 我想知道如何使用MyBatis 3和Spring 3使用insert语句实现批处理操作?
例如,以下是目前正在进行的操作:
spring.xml:
<bean id="jndiTemplateDatasource" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${context.factory}</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplateDatasource"/>
<property name="jndiName" value="${connectionpool.jndi}"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test" />
</bean>
Run Code Online (Sandbox Code Playgroud)
MyService.xml:
<insert id="insertMyRecord" parameterType="com.test.MyRecord" >
insert into ... // code removed
</insert>
Run Code Online (Sandbox Code Playgroud)
MyService.java:
public interface MyService {
public void insertMyRecord (MyRecord);
}
Run Code Online (Sandbox Code Playgroud)
MyController.java:
@Controller
public class MyController …Run Code Online (Sandbox Code Playgroud) 我正在使用Mybatis(用于持久化java到数据库)和Mybatis Generator(用于从数据库模式自动生成映射器xml文件和java接口)的项目.
Mybatis生成器在生成基本crud操作所需的文件方面做得很好.
上下文
对于某些表/类,我们需要比MyBatis Generator工具生成的"crud stuff"更多的"东西"(代码查询等).
有没有办法拥有"两全其美",即使用自动生成以及"自定义代码".如何分离和构建"手动编辑的文件"和"自动生成的文件".
提案
我正在考虑以下内容,即表格"Foo"
自动生成
(其中"Crud"代表"创建读取更新删除")
手编辑
概念:如果架构发生了变化,您可以随时安全地自动生成"Crud"xml和.java文件,而不会消除任何自定义更改.
问题
mybatis会正确处理这种情况,即这个映射器是否会正确执行自动生成的"crud代码"?
FooMapper fooMapper = sqlSession.getMapper(FooMapper.class);
你推荐什么方法?
编辑1:*我们的数据库设计使用"核心表"("元素"),其他表"扩展"该表并添加额外的属性(共享密钥).我查看了文档和来源得出结论,我不能将Mybatis Generator与这样的'扩展'结合使用而不需要任何手动编辑:
即这不起作用.-ElementMapper扩展"ElementCrudMapper"-FooMapper.xml扩展"ElementCrudMapper"和"FooCrudMapper"
谢谢大家!
我试图在MyBatis中选择一个时间戳并将其作为LocalDateTime(来自joda-time)返回.
如果我尝试将结果作为a返回,我的配置工作正常java.sql.Timestamp.我证明我的类型处理程序工作正常:如果我LocalDateTime在MyBatis映射器文件中使用包含as only字段和resultMap 的包装类,我会得到正确的结果.
但是,当我尝试为这个select 指定org.joda.time.LocalDateTimeas时resultType,我总是得到null,好像忽略了类型处理程序.
据我所知,在我拥有的情况下,MyBatis使用默认的typeHandler resultType="java.sql.Timestamp".因此,我希望它使用我在会议时配置的typeHandler之一resultType="org.joda.time.LocalDateTime".
我错过了什么?有没有办法使用我的typeHandler或者我被迫创建一个包装类和resultMap?这是我的后备解决方案,但我想尽可能避免它.
任何帮助赞赏.谢谢.
MyBatis的-config.xml中
<?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>
<typeHandlers>
<typeHandler javaType="org.joda.time.LocalDate" jdbcType="DATE" handler="...LocalDateTypeHandler"/>
<typeHandler javaType="org.joda.time.LocalDateTime" jdbcType="TIMESTAMP" handler="...LocalDateTimeTypeHandler"/>
</typeHandlers>
</configuration>
Run Code Online (Sandbox Code Playgroud)
NotifMailDao.java
import org.joda.time.LocalDateTime;
public interface NotifMailDao {
LocalDateTime getLastNotifTime(String userId);
}
Run Code Online (Sandbox Code Playgroud)
NotifMailDao.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="lu.bgl.notif.mail.dao.NotifMailDao">
<select id="getLastNotifTime" resultType="org.joda.time.LocalDateTime">
SELECT current_timestamp
AS last_time
FROM DUAL
</select> …Run Code Online (Sandbox Code Playgroud)