Skip to content

Commit

Permalink
添加了对原生myBatis查询的JPA映射支持
Browse files Browse the repository at this point in the history
  • Loading branch information
trifolium-x committed Apr 18, 2024
1 parent 6cdb916 commit 9a34553
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 4 deletions.
16 changes: 16 additions & 0 deletions base/src/test/java/tk/mybatis/mapper/rawresultmap/CreateDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
drop table user if exists;

create table user
(
id integer NOT NULL PRIMARY KEY,
name varchar(32),
user_name varchar(32),
email varchar(32),
age__int__aa integer,
create_time datetime
);

INSERT INTO user (id, name, user_name, email, age__int__aa, create_time)
VALUES (1, 'trifolium1', 'wang1', 'email1', 23, now());
INSERT INTO user (id, name, user_name, email, age__int__aa, create_time)
VALUES (2, 'trifolium2', 'wang2', 'email2', 32, now());
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package tk.mybatis.mapper.rawresultmap;

import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.mapper.base.BaseTest;
import tk.mybatis.mapper.code.Style;
import tk.mybatis.mapper.entity.Config;

import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.List;

/**
* @author liuzh
*/
public class RawResultMapTest extends BaseTest {

@Override
protected Config getConfig() {
Config config = super.getConfig();
config.setStyle(Style.normal);
return config;
}

@Override
protected Reader getConfigFileAsReader() throws IOException {
return toReader(RawResultMapTest.class.getResource("mybatis-config-rawresultmap.xml"));
}

@Override
protected Reader getSqlFileAsReader() throws IOException {
URL url = RawResultMapTest.class.getResource("CreateDB.sql");
return toReader(url);
}

@Test
public void testSelect() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);

List<User> users;

System.out.println("------selectAll------");
users = mapper.selectAll();
users.forEach(u -> {
System.out.println(u);
Assert.assertNotNull(u.getUname());
Assert.assertNotNull(u.getAge());
Assert.assertNotNull(u.getCreateTime());
Assert.assertNull(u.getEmail());
});
System.out.println("------------");

System.out.println("------selectRawAnnotation------");
users = mapper.selectRawAnnotation();
users.forEach(u -> {
System.out.println(u);
Assert.assertNotNull(u.getUname());
Assert.assertNotNull(u.getAge());
Assert.assertNotNull(u.getCreateTime());
Assert.assertNotNull(u.getEmail());
});
System.out.println("------------");

System.out.println("------fetchRawResultMap------");
users = mapper.fetchRawResultMap();
users.forEach(u -> {
System.out.println(u);
Assert.assertNotNull(u.getUname());
Assert.assertNotNull(u.getAge());
Assert.assertNotNull(u.getCreateTime());
Assert.assertNotNull(u.getEmail());
});
System.out.println("------------");

System.out.println("------fetchRawResultType------");
users = mapper.fetchRawResultType();
users.forEach(u -> {
System.out.println(u);
Assert.assertNotNull(u.getUname());
Assert.assertNotNull(u.getAge());
Assert.assertNotNull(u.getCreateTime());
Assert.assertNotNull(u.getEmail());
});
System.out.println("------------");

System.out.println("------getMapUser------");
System.out.println(mapper.getMapUser());
System.out.println("------------");

System.out.println(mapper.selectCount2());
} finally {
sqlSession.close();
}
}
}
94 changes: 94 additions & 0 deletions base/src/test/java/tk/mybatis/mapper/rawresultmap/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package tk.mybatis.mapper.rawresultmap;

import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.code.Style;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;

/**
* @author liuzh
*/
@NameStyle(Style.camelhump)
@Table(name = "user")
public class User {

@Id
private Integer id;

private String name;

@Column(name = "user_name")
private String uname;

@Column(name = "age__int__aa")
private Integer age;

private Date createTime;

@Transient
private String email;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUname() {
return uname;
}

public void setUname(String uname) {
this.uname = uname;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", uname='" + uname + '\'' +
", age=" + age +
", createTime=" + createTime +
", email='" + email + '\'' +
'}';
}
}
25 changes: 25 additions & 0 deletions base/src/test/java/tk/mybatis/mapper/rawresultmap/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tk.mybatis.mapper.rawresultmap;

import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.BaseMapper;

import java.util.List;
import java.util.Map;

/**
* @author liuzh
*/
public interface UserMapper extends BaseMapper<User> {


@Select("SELECT * FROM user")
List<User> selectRawAnnotation();

List<User> fetchRawResultType();

List<User> fetchRawResultMap();

Map<String, Object> getMapUser();

Integer selectCount2();
}
32 changes: 32 additions & 0 deletions base/src/test/java/tk/mybatis/mapper/rawresultmap/UserMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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="tk.mybatis.mapper.rawresultmap.UserMapper">
<resultMap id="BaseResultMap" type="tk.mybatis.mapper.rawresultmap.User">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="user_name" jdbcType="VARCHAR" property="uname" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="age____aa" jdbcType="INTEGER" property="age" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>

<select id="fetchRawResultType" resultType = "tk.mybatis.mapper.rawresultmap.User">
select * from user
</select>

<select id="fetchRawResultMap" resultMap="BaseResultMap">
select * from user
</select>

<select id="getMapUser" resultType="Map">
select * from user where id = 1
</select>

<select id="selectCount2" resultType="java.lang.Integer">
SELECT count(1) from user
</select>

</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:rawresultmap"/>
<property name="username" value="sa"/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper class="tk.mybatis.mapper.rawresultmap.UserMapper"/>
<mapper resource="tk/mybatis/mapper/rawresultmap/UserMapper.xml"/>
</mappers>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public static EntityTable getEntityTable(Class<?> entityClass) {
return entityTable;
}

/**
* 获取表对象,如果没有则返回Null
*
* @param entityClass
* @return
*/
public static EntityTable getEntityTableOrNull(Class<?> entityClass) {
return entityTableMap.get(entityClass);
}

/**
* 获取默认的orderby语句
*
Expand Down
Loading

0 comments on commit 9a34553

Please sign in to comment.