Skip to content

Commit e3a45f5

Browse files
baljeet20maibin
authored andcommitted
BAEL-788 A guid to mybatis (eugenp#1637)
* BAEL-788 A guid to mybatis * BAEL-788 A guid to mybatis
1 parent 2f34e89 commit e3a45f5

9 files changed

Lines changed: 403 additions & 0 deletions

File tree

‎mybatis/pom.xml‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>mybatis</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<properties>
11+
<junit.version>4.12</junit.version>
12+
<derby.version>10.13.1.1</derby.version>
13+
<mybatis.version>3.2.2</mybatis.version>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.apache.derby</groupId>
18+
<artifactId>derby</artifactId>
19+
<version>${derby.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.mybatis</groupId>
23+
<artifactId>mybatis</artifactId>
24+
<version>${mybatis.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>junit</groupId>
28+
<artifactId>junit</artifactId>
29+
<version>${junit.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
34+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.mybatis.mapper;
2+
3+
import com.baeldung.mybatis.model.Address;
4+
import com.baeldung.mybatis.model.Person;
5+
import org.apache.ibatis.annotations.*;
6+
7+
8+
public interface AddressMapper {
9+
10+
@Insert("Insert into address (streetAddress,personId) values(#{streetAddress},#{personId})")
11+
@Options(useGeneratedKeys = true,flushCache=true )
12+
public Integer saveAddress(Address address);
13+
14+
@Select("SELECT addressId, streetAddress FROM Address WHERE addressId = #{addressId}")
15+
@Results(value = {
16+
@Result(property = "addressId", column = "addressId"),
17+
@Result(property = "streetAddress", column = "streetAddress"),
18+
@Result(property = "person", column = "personId",javaType =Person.class,one=@One(select = "getPerson"))
19+
})
20+
Address getAddresses(Integer addressID);
21+
22+
@Select("SELECT personId FROM address WHERE addressId = #{addressId})")
23+
Person getPerson(Integer personId);
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.mybatis.mapper;
2+
3+
import com.baeldung.mybatis.model.Address;
4+
import com.baeldung.mybatis.model.Person;
5+
import com.baeldung.mybatis.utils.MyBatisUtil;
6+
import org.apache.ibatis.annotations.*;
7+
import org.apache.ibatis.mapping.StatementType;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
13+
public interface PersonMapper {
14+
15+
@Insert("Insert into person(name) values (#{name})")
16+
public Integer save(Person person);
17+
18+
@Update("Update Person set name= #{name} where personId=#{personId}")
19+
public void updatePerson(Person person);
20+
21+
@Delete("Delete from Person where personId=#{personId}")
22+
public void deletePersonById(Integer personId);
23+
24+
@Select("SELECT person.personId, person.name FROM person WHERE person.personId = #{personId}")
25+
Person getPerson(Integer personId);
26+
27+
@Select("Select personId,name from Person where personId=#{personId}")
28+
@Results(value ={
29+
@Result(property = "personId", column = "personId"),
30+
@Result(property="name", column = "name"),
31+
@Result(property = "addresses",javaType = List.class,column = "personId",
32+
many=@Many(select = "getAddresses"))
33+
34+
})
35+
public Person getPersonById(Integer personId);
36+
37+
@Select("select addressId,streetAddress,personId from address where personId=#{personId}")
38+
public Address getAddresses(Integer personId);
39+
40+
@Select("select * from Person ")
41+
@MapKey("personId")
42+
Map<Integer,Person> getAllPerson();
43+
44+
@SelectProvider(type=MyBatisUtil.class,method="getPersonByName")
45+
public Person getPersonByName(String name);
46+
47+
48+
@Select(value= "{ CALL getPersonByProc( #{personId, mode=IN, jdbcType=INTEGER})}")
49+
@Options(statementType = StatementType.CALLABLE)
50+
public Person getPersonByProc(Integer personId);
51+
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.mybatis.model;
2+
3+
4+
public class Address {
5+
6+
private Integer addressId;
7+
private String streetAddress;
8+
private Integer personId;
9+
10+
public Address() {
11+
}
12+
13+
public Integer getPersonId() {
14+
return personId;
15+
}
16+
17+
public void setPersonId(Integer personId) {
18+
this.personId = personId;
19+
}
20+
21+
22+
23+
public Address(String streetAddress) {
24+
this.streetAddress =streetAddress;
25+
}
26+
27+
public Person getPerson() {
28+
return person;
29+
}
30+
31+
public void setPerson(Person person) {
32+
this.person = person;
33+
}
34+
35+
private Person person;
36+
37+
public Address(int i, String name) {
38+
this.streetAddress = name;
39+
}
40+
41+
public Integer getAddressId() {
42+
return addressId;
43+
}
44+
45+
public String getStreetAddress() {
46+
return streetAddress;
47+
}
48+
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.mybatis.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
7+
public class Person {
8+
9+
private Integer personId;
10+
private String name;
11+
private List<Address> addresses;
12+
13+
public Person() {
14+
}
15+
16+
public Person(Integer personId, String name) {
17+
this.personId=personId;
18+
this.name = name;
19+
addresses = new ArrayList<Address>();
20+
}
21+
22+
public Person(String name) {
23+
this.name=name;
24+
}
25+
26+
public Integer getPersonId() {
27+
return personId;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
public void addAddress(Address address){
34+
addresses.add(address);
35+
}
36+
37+
public List<Address> getAddresses() {
38+
return addresses;
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.mybatis.utils;
2+
import org.apache.ibatis.io.Resources;
3+
import org.apache.ibatis.jdbc.SQL;
4+
import org.apache.ibatis.session.SqlSessionFactory;
5+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
10+
public class MyBatisUtil {
11+
private static SqlSessionFactory sqlSessionFactory;
12+
static {
13+
String resource = "mybatis-config.xml";
14+
InputStream inputStream;
15+
try {
16+
inputStream = Resources.getResourceAsStream(resource);
17+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
18+
} catch (IOException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
public static SqlSessionFactory getSqlSessionFactory(){
23+
return sqlSessionFactory;
24+
}
25+
26+
public String getPersonByName(String name){
27+
return new SQL(){{
28+
SELECT("*");
29+
FROM("person");
30+
WHERE("name like #{name} || '%'");
31+
}}.toString();
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
<configuration>
6+
<environments default="development">
7+
<environment id="development">
8+
<transactionManager type="JDBC"/>
9+
<dataSource type="POOLED">
10+
<property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
11+
<property name="url" value="jdbc:derby:testdb1;create=true"/>
12+
<property name="username" value="sa"/>
13+
<property name="password" value="pass123"/>
14+
</dataSource>
15+
</environment>
16+
</environments>
17+
<mappers>
18+
<mapper class="com.baeldung.mybatis.mapper.PersonMapper"/>
19+
<mapper class="com.baeldung.mybatis.mapper.AddressMapper"/>
20+
</mappers>
21+
</configuration>

0 commit comments

Comments
 (0)