BAEL-788 A guid to mybatis (#1637)
* BAEL-788 A guid to mybatis * BAEL-788 A guid to mybatis
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.mybatis.mapper;
|
||||
|
||||
import com.baeldung.mybatis.model.Address;
|
||||
import com.baeldung.mybatis.model.Person;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
|
||||
public interface AddressMapper {
|
||||
|
||||
@Insert("Insert into address (streetAddress,personId) values(#{streetAddress},#{personId})")
|
||||
@Options(useGeneratedKeys = true,flushCache=true )
|
||||
public Integer saveAddress(Address address);
|
||||
|
||||
@Select("SELECT addressId, streetAddress FROM Address WHERE addressId = #{addressId}")
|
||||
@Results(value = {
|
||||
@Result(property = "addressId", column = "addressId"),
|
||||
@Result(property = "streetAddress", column = "streetAddress"),
|
||||
@Result(property = "person", column = "personId",javaType =Person.class,one=@One(select = "getPerson"))
|
||||
})
|
||||
Address getAddresses(Integer addressID);
|
||||
|
||||
@Select("SELECT personId FROM address WHERE addressId = #{addressId})")
|
||||
Person getPerson(Integer personId);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.mybatis.mapper;
|
||||
|
||||
import com.baeldung.mybatis.model.Address;
|
||||
import com.baeldung.mybatis.model.Person;
|
||||
import com.baeldung.mybatis.utils.MyBatisUtil;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
import org.apache.ibatis.mapping.StatementType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public interface PersonMapper {
|
||||
|
||||
@Insert("Insert into person(name) values (#{name})")
|
||||
public Integer save(Person person);
|
||||
|
||||
@Update("Update Person set name= #{name} where personId=#{personId}")
|
||||
public void updatePerson(Person person);
|
||||
|
||||
@Delete("Delete from Person where personId=#{personId}")
|
||||
public void deletePersonById(Integer personId);
|
||||
|
||||
@Select("SELECT person.personId, person.name FROM person WHERE person.personId = #{personId}")
|
||||
Person getPerson(Integer personId);
|
||||
|
||||
@Select("Select personId,name from Person where personId=#{personId}")
|
||||
@Results(value ={
|
||||
@Result(property = "personId", column = "personId"),
|
||||
@Result(property="name", column = "name"),
|
||||
@Result(property = "addresses",javaType = List.class,column = "personId",
|
||||
many=@Many(select = "getAddresses"))
|
||||
|
||||
})
|
||||
public Person getPersonById(Integer personId);
|
||||
|
||||
@Select("select addressId,streetAddress,personId from address where personId=#{personId}")
|
||||
public Address getAddresses(Integer personId);
|
||||
|
||||
@Select("select * from Person ")
|
||||
@MapKey("personId")
|
||||
Map<Integer,Person> getAllPerson();
|
||||
|
||||
@SelectProvider(type=MyBatisUtil.class,method="getPersonByName")
|
||||
public Person getPersonByName(String name);
|
||||
|
||||
|
||||
@Select(value= "{ CALL getPersonByProc( #{personId, mode=IN, jdbcType=INTEGER})}")
|
||||
@Options(statementType = StatementType.CALLABLE)
|
||||
public Person getPersonByProc(Integer personId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.mybatis.model;
|
||||
|
||||
|
||||
public class Address {
|
||||
|
||||
private Integer addressId;
|
||||
private String streetAddress;
|
||||
private Integer personId;
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public Integer getPersonId() {
|
||||
return personId;
|
||||
}
|
||||
|
||||
public void setPersonId(Integer personId) {
|
||||
this.personId = personId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Address(String streetAddress) {
|
||||
this.streetAddress =streetAddress;
|
||||
}
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
private Person person;
|
||||
|
||||
public Address(int i, String name) {
|
||||
this.streetAddress = name;
|
||||
}
|
||||
|
||||
public Integer getAddressId() {
|
||||
return addressId;
|
||||
}
|
||||
|
||||
public String getStreetAddress() {
|
||||
return streetAddress;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.mybatis.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class Person {
|
||||
|
||||
private Integer personId;
|
||||
private String name;
|
||||
private List<Address> addresses;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(Integer personId, String name) {
|
||||
this.personId=personId;
|
||||
this.name = name;
|
||||
addresses = new ArrayList<Address>();
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name=name;
|
||||
}
|
||||
|
||||
public Integer getPersonId() {
|
||||
return personId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void addAddress(Address address){
|
||||
addresses.add(address);
|
||||
}
|
||||
|
||||
public List<Address> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.mybatis.utils;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.jdbc.SQL;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class MyBatisUtil {
|
||||
private static SqlSessionFactory sqlSessionFactory;
|
||||
static {
|
||||
String resource = "mybatis-config.xml";
|
||||
InputStream inputStream;
|
||||
try {
|
||||
inputStream = Resources.getResourceAsStream(resource);
|
||||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static SqlSessionFactory getSqlSessionFactory(){
|
||||
return sqlSessionFactory;
|
||||
}
|
||||
|
||||
public String getPersonByName(String name){
|
||||
return new SQL(){{
|
||||
SELECT("*");
|
||||
FROM("person");
|
||||
WHERE("name like #{name} || '%'");
|
||||
}}.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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>
|
||||
<environments default="development">
|
||||
<environment id="development">
|
||||
<transactionManager type="JDBC"/>
|
||||
<dataSource type="POOLED">
|
||||
<property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
|
||||
<property name="url" value="jdbc:derby:testdb1;create=true"/>
|
||||
<property name="username" value="sa"/>
|
||||
<property name="password" value="pass123"/>
|
||||
</dataSource>
|
||||
</environment>
|
||||
</environments>
|
||||
<mappers>
|
||||
<mapper class="com.baeldung.mybatis.mapper.PersonMapper"/>
|
||||
<mapper class="com.baeldung.mybatis.mapper.AddressMapper"/>
|
||||
</mappers>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user