-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #915 from trifolium-x/dev
添加了对原生MyBatis查询的JPA注解映射支持
- Loading branch information
Showing
9 changed files
with
347 additions
and
4 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
base/src/test/java/tk/mybatis/mapper/rawresultmap/CreateDB.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
99 changes: 99 additions & 0 deletions
99
base/src/test/java/tk/mybatis/mapper/rawresultmap/RawResultMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
94
base/src/test/java/tk/mybatis/mapper/rawresultmap/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
base/src/test/java/tk/mybatis/mapper/rawresultmap/UserMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
base/src/test/java/tk/mybatis/mapper/rawresultmap/UserMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
28 changes: 28 additions & 0 deletions
28
base/src/test/java/tk/mybatis/mapper/rawresultmap/mybatis-config-rawresultmap.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.