决定将我的一个项目从iBatis移到MyBatis并遇到插入问题.
mapper 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="bap.persistance.interfaces.ArticleMapper">
<insert id="insertTestA">
insert into test_a ( cookie ) values( 'tomek pilot');
</insert>
</mapper>
Run Code Online (Sandbox Code Playgroud)
mapper java文件:
public interface ArticleMapper {
void insertTestA();
}
Run Code Online (Sandbox Code Playgroud)
映射器实现:
String resource = "bap/persistance/MyBatis_xml/MyBatisConfig.xml";
....
...
public void createArticle( Article article ) throws IOException {
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
try{
ArticleMapper mapper = session.getMapper(ArticleMapper.class);
mapper.insertTestA();
} catch( Exception e ){
e.printStackTrace();
} …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) 我有一个用MyBatis映射文件写的sql,它是这样的:
<select id="somesql">
select a,b,c from tbl_name where d = ?
</select>
Run Code Online (Sandbox Code Playgroud)
占位符值d应该是在名为的文件中声明的常量Constants.java:
public static final String d = "d_value";
Run Code Online (Sandbox Code Playgroud)
如何在不实际传递<select>构造中的参数的情况下用值替换占位符
?我试过#{com.pkg.name.Constants.d}但它没用.
没有硬编码!!!
我目前正在开发一个电子商务应用程序,我必须使用搜索功能显示可用产品列表.
与每次搜索一样,我必须在这里实现分页.
我使用mybatis作为我的ORM工具,使用mysql作为底层数据库.
谷歌搜索我找到了以下方法来完成这项任务:
客户端分页 :在这里,我将不得不一次性从匹配搜索条件的数据库中获取所有结果,并在我的代码级别处理分页(可能是最终代码).
服务器端分页:使用mysql,我可以使用Limit和结果集的偏移量来构造如下的查询:
SELECT * FROM sampletable WHERE condition1>1 AND condition2>2 LIMIT 0,20
在这里,每次用户在搜索结果中导航时选择新页面时,我都必须传递偏移和限制计数.
谁能告诉,
任何输入都是高度适应的.谢谢 .
我使用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) 我已经阅读了关于Mybatis的书和文档,XML和Annotation都做了我想要的,但是从myBatis官方网站,他们声称XML是一种更好的做Mappers的方法,因为Java注释有局限性.
我个人更喜欢注释,例如
public interface PersonDAO {
String INSERT_PERSON = "insert into person (title,firstName,surName,jobTitle,dob,email,mobile,landPhone,fax,twitter,facebook,linkedin) VALUES (#{title},#{firstName},#{surName},#{jobTitle},#{dob},#{email},#{mobile},#{landPhone},#{fax},#{twitter},#{facebook},#{linkedin})";
String UPDATE_PERSON = "update person set title=#{title},firstName=#{firstName},surName=#{surName},jobTitle=#{jobTitle},dob=#{dob},email=#{email},mobile=#{mobile},landPhone=#{landPhone},fax=#{fax},twitter=#{twitter},facebook=#{facebook},linkedin=#{linkedin} where id=#{id}";
String GET_PERSON_BY_ID = "SELECT * FROM vw_person WHERE id = #{personId}";
String DELETE_PERSON = "DELETE FROM person WHERE id = #{personId}";
@Select(GET_PERSON_BY_ID)
public PersonVO doSelectPerson(long personId) throws Exception;
@Update(UPDATE_PERSON)@Options(flushCache = true, useCache = true)
public int doUpdatePerson(PersonVO vo) throws Exception;
@Insert(INSERT_PERSON)@Options(useGeneratedKeys = true, keyProperty = "id", flushCache = true)
public int doCreatePerson(PersonVO person) throws Exception;
@Delete(DELETE_PERSON)@Options(flushCache …Run Code Online (Sandbox Code Playgroud) 在Mybatis xml映射文件中,我尝试为User表编写更新查询,如下所示.每个输入参数都可以为null,我只在它不为null时才更新.你不知道哪个'if'条件可以通过,哪个可能是最后一个,因此必须在每个语句中添加逗号.
问题是额外的','会导致查询异常.似乎Mybatis不会忽略额外的逗号.
我的解决方法是将"id =#{id}"放在最后修复问题,但这是多余的.
什么是真正的解决方案?
代码:
<update id="update" parameterType="User">
UPDATE user SET
<if test="username != null">
username = #{username},
</if>
<if test="password != null">
password = #{password},
</if>
<if test="email != null">
email = #{email},
</if>
id= #{id} // this is redundant
WHERE id = #{id}
</update>
Run Code Online (Sandbox Code Playgroud)
PS:我使用的环境是:Java Spring + MyBatis + MySQL.
通常,myBatis的select方法返回单个对象或通用List类型.例如,我想取一个班级的所有学生:
<select id="fetchStudentsOfClass" parameterType="int" resultMap="resultMapStudent">
SELECT * FROM students WHERE class_id=#{id}
</select>
Run Code Online (Sandbox Code Playgroud)
我很容易得到这样的结果:List<Student>.
现在,如果我想得到这样的结果而不是List<Student>:
class MyClass
{
List<Student> getStudents{return this.students;}
void setStudents(List<Student> students){this.students = students}
private List<Student> students;
}
Run Code Online (Sandbox Code Playgroud)
我能怎么做?
MyBatis有任何功能允许从像Hibernate那样的de Class Model创建SQL模式吗?
我在谷歌寻找,我只找到有关MyBatis Generator的信息(http://mybatis.github.io/generator/).这个工具似乎对从SQL Schema生成Java模型很有用,这与我想要的相反.
我正在使用mybatis-spring 1.2.3和Spring4来创建一个Web应用程序.主数据存储是MySQL在生产环境中,但我也在内存数据库H2中进行单元测试.
MyBatis在测试和生产中都能很好地兼容MySQL和H2,但是我遇到了一个问题,有一天我需要force index(idx1)在MySQL的查询中使用它,这将导致单元测试中的语法错误,因为H2不支持force index.结果,单元测试完全被打破.
我想知道MyBatis有什么方法可以处理这种情况吗?(数据库类型在测试和生产方面有所不同,它们对SQL语法的支持也不尽相同.)
这是我的映射文件:
<?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="myproject.mapper.UserMapper">
<select id="getGameUsersForDate" resultType="myproject.dao.domain.GameUser">
select
*
from game_user
force index(idx1)
where
game_id in
<choose>
<when test="gameIds.size() > 0">
<foreach item="gameId" collection="gameIds" open="(" separator="," close=")">
#{gameId}
</foreach>
</when>
<otherwise>
(null)
</otherwise>
</choose>
and uid in
<choose>
<when test="uids.size() > 0">
<foreach item="uid" collection="mids" open="(" separator="," close=")">
#{mid}
</foreach>
</when>
<otherwise>
(null)
</otherwise>
</choose>
and `date` = #{date}
</select>
</mapper>
Run Code Online (Sandbox Code Playgroud) mybatis ×10
java ×7
ibatis ×4
hibernate ×2
mysql ×2
sql ×2
constants ×1
h2 ×1
pagination ×1
parameters ×1
spring-boot ×1