minor formatting cleanuop

This commit is contained in:
eugenp 2017-04-18 11:27:58 +03:00
parent 1aff8ca17d
commit 6c181db567
6 changed files with 215 additions and 228 deletions

View File

@ -4,21 +4,18 @@ 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);
@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 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);
@Select("SELECT personId FROM address WHERE addressId = #{addressId})")
Person getPerson(Integer personId);
}

View File

@ -9,44 +9,39 @@ 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);
@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);
@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);
@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 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"))
@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);
})
public Person getPersonById(Integer personId);
@Select("select addressId,streetAddress,personId from address where personId=#{personId}")
public Address getAddresses(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();
@Select("select * from Person ")
@MapKey("personId")
Map<Integer, Person> getAllPerson();
@SelectProvider(type=MyBatisUtil.class,method="getPersonByName")
public Person getPersonByName(String name);
@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);
@Select(value = "{ CALL getPersonByProc( #{personId, mode=IN, jdbcType=INTEGER})}")
@Options(statementType = StatementType.CALLABLE)
public Person getPersonByProc(Integer personId);
}

View File

@ -1,49 +1,46 @@
package com.baeldung.mybatis.model;
public class Address {
private Integer addressId;
private String streetAddress;
private Integer personId;
private Integer addressId;
private String streetAddress;
private Integer personId;
public Address() {
}
public Address() {
}
public Integer getPersonId() {
return personId;
}
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public Address(String streetAddress) {
this.streetAddress = streetAddress;
}
public Person getPerson() {
return person;
}
public Address(String streetAddress) {
this.streetAddress =streetAddress;
}
public void setPerson(Person person) {
this.person = person;
}
public Person getPerson() {
return person;
}
private Person person;
public void setPerson(Person person) {
this.person = person;
}
public Address(int i, String name) {
this.streetAddress = name;
}
private Person person;
public Integer getAddressId() {
return addressId;
}
public Address(int i, String name) {
this.streetAddress = name;
}
public Integer getAddressId() {
return addressId;
}
public String getStreetAddress() {
return streetAddress;
}
public String getStreetAddress() {
return streetAddress;
}
}

View File

@ -3,38 +3,38 @@ 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;
private Integer personId;
private String name;
private List<Address> addresses;
public Person() {
}
public Person() {
}
public Person(Integer personId, String name) {
this.personId=personId;
this.name = name;
addresses = new ArrayList<Address>();
}
public Person(Integer personId, String name) {
this.personId = personId;
this.name = name;
addresses = new ArrayList<Address>();
}
public Person(String name) {
this.name=name;
}
public Person(String name) {
this.name = name;
}
public Integer getPersonId() {
return personId;
}
public Integer getPersonId() {
return personId;
}
public String getName() {
return name;
}
public void addAddress(Address address){
addresses.add(address);
}
public String getName() {
return name;
}
public List<Address> getAddresses() {
return addresses;
}
public void addAddress(Address address) {
addresses.add(address);
}
public List<Address> getAddresses() {
return addresses;
}
}

View File

@ -16,33 +16,35 @@ import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtil {
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String URL = "jdbc:derby:testdb1;create=true";
public static final String USERNAME = "sa";
public static final String PASSWORD = "pass123";
private static SqlSessionFactory sqlSessionFactory;
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String URL = "jdbc:derby:testdb1;create=true";
public static final String USERNAME = "sa";
public static final String PASSWORD = "pass123";
private static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory buildqlSessionFactory(){
DataSource dataSource=new PooledDataSource(DRIVER, URL, USERNAME, PASSWORD);
Environment environment=new Environment("Development",new JdbcTransactionFactory(),dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(PersonMapper.class);
configuration.addMapper(AddressMapper.class);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(configuration);
return factory;
public static SqlSessionFactory buildqlSessionFactory() {
DataSource dataSource = new PooledDataSource(DRIVER, URL, USERNAME, PASSWORD);
Environment environment = new Environment("Development", new JdbcTransactionFactory(), dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(PersonMapper.class);
configuration.addMapper(AddressMapper.class);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(configuration);
return factory;
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public String getPersonByName(String name){
return new SQL(){{
SELECT("*");
FROM("person");
WHERE("name like #{name} || '%'");
}}.toString();
}
public String getPersonByName(String name) {
return new SQL() {
{
SELECT("*");
FROM("person");
WHERE("name like #{name} || '%'");
}
}.toString();
}
}

View File

@ -18,132 +18,128 @@ import static junit.framework.TestCase.assertEquals;
public class PersonMapperTest {
SqlSession session;
SqlSession session;
@Before
public void setup() throws SQLException {
@Before
public void setup() throws SQLException {
session = MyBatisUtil.buildqlSessionFactory().openSession();
createTables(session);
session = MyBatisUtil.buildqlSessionFactory().openSession();
createTables(session);
}
}
private void createTables(SqlSession session) throws SQLException {
private void createTables(SqlSession session) throws SQLException {
String createPersonTable = "create table person ("
+ "personId integer not null generated always as"
+ " identity (start with 1, increment by 1), "
+ "name varchar(30) not null, "
+ "constraint primary_key_person primary key (personId))";
String createPersonTable = "create table person (" + "personId integer not null generated always as"
+ " identity (start with 1, increment by 1), " + "name varchar(30) not null, "
+ "constraint primary_key_person primary key (personId))";
String createAddressTable = "create table address ("
+ "addressId integer not null generated always as"
+ " identity (start with 1, increment by 1), "
+ "streetAddress varchar(300), personId integer, "
+ "constraint primary_key_address primary key (addressId))";
String createAddressTable = "create table address (" + "addressId integer not null generated always as"
+ " identity (start with 1, increment by 1), " + "streetAddress varchar(300), personId integer, "
+ "constraint primary_key_address primary key (addressId))";
String alterTable="ALTER TABLE " +
" address ADD CONSTRAINT fk_person FOREIGN KEY (personId) REFERENCES person (personId)";
String alterTable = "ALTER TABLE "
+ " address ADD CONSTRAINT fk_person FOREIGN KEY (personId) REFERENCES person (personId)";
session.getConnection().createStatement().execute(createPersonTable);
session.getConnection().createStatement().execute(createAddressTable);
session.getConnection().createStatement().execute(alterTable);
session.getConnection().createStatement().execute(createPersonTable);
session.getConnection().createStatement().execute(createAddressTable);
session.getConnection().createStatement().execute(alterTable);
}
}
@Test
public void whenPersonAdressSaved_ThenPersonAddressCanBeQueried() {
Person person = new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Integer id = personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
@Test
public void whenPersonAdressSaved_ThenPersonAddressCanBeQueried(){
Person person=new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Integer id =personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper=session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
Person returnedPerson = personMapper.getPersonById(id);
assertEquals("Baljeet S", returnedPerson.getName());
assertEquals("Pune", returnedPerson.getAddresses().get(0).getStreetAddress());
}
Person returnedPerson= personMapper.getPersonById(id);
assertEquals("Baljeet S", returnedPerson.getName());
assertEquals("Pune", returnedPerson.getAddresses().get(0).getStreetAddress());
}
@Test
public void whenPersonSaved_ThenPersonCanBeQueried() {
Person person = new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Integer id = personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
@Test
public void whenPersonSaved_ThenPersonCanBeQueried(){
Person person=new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Integer id =personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper=session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
Person returnedPerson = personMapper.getPerson(id);
assertEquals("Baljeet S", returnedPerson.getName());
}
Person returnedPerson= personMapper.getPerson(id);
assertEquals("Baljeet S", returnedPerson.getName());
}
@Test
public void whenPersonUpdated_ThenPersonIsChanged() {
Person person = new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Integer id = personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
@Test
public void whenPersonUpdated_ThenPersonIsChanged(){
Person person=new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Integer id =personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper=session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
personMapper.updatePerson(new Person(id, "Baljeet1"));
Person returnedPerson = personMapper.getPerson(id);
assertEquals("Baljeet1", returnedPerson.getName());
}
personMapper.updatePerson(new Person(id,"Baljeet1"));
Person returnedPerson= personMapper.getPerson(id);
assertEquals("Baljeet1", returnedPerson.getName());
}
@Test
public void whenPersoSaved_ThenMapIsReturned(){
Person person=new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Integer id =personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper=session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
@Test
public void whenPersoSaved_ThenMapIsReturned() {
Person person = new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Integer id = personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
Map<Integer, Person> returnedPerson= personMapper.getAllPerson();
assertEquals(1, returnedPerson.size());
}
Map<Integer, Person> returnedPerson = personMapper.getAllPerson();
assertEquals(1, returnedPerson.size());
}
@Test
public void whenPersonSearched_ThenResultIsReturned(){
Person person=new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Integer id =personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper=session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
@Test
public void whenPersonSearched_ThenResultIsReturned() {
Person person = new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Integer id = personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
addressMapper.saveAddress(address);
Person returnedPerson= personMapper.getPersonByName("Baljeet S");
assertEquals("Baljeet S", returnedPerson.getName());
}
Person returnedPerson = personMapper.getPersonByName("Baljeet S");
assertEquals("Baljeet S", returnedPerson.getName());
}
@Test
public void whenAddressSearched_ThenResultIsReturned(){
Person person=new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Integer id =personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper=session.getMapper(AddressMapper.class);
Integer addressId=addressMapper.saveAddress(address);
Address returnedAddress=addressMapper.getAddresses(addressId);
@Test
public void whenAddressSearched_ThenResultIsReturned() {
Person person = new Person("Baljeet S");
Address address = new Address("Pune");
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Integer id = personMapper.save(person);
address.setPersonId(id);
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
Integer addressId = addressMapper.saveAddress(address);
Address returnedAddress = addressMapper.getAddresses(addressId);
assertEquals("Pune", returnedAddress.getStreetAddress());
}
assertEquals("Pune", returnedAddress.getStreetAddress());
}
@After
public void cleanup() throws SQLException {
session.getConnection().createStatement().execute("drop table address");
session.getConnection().createStatement().execute("drop table person");
@After
public void cleanup() throws SQLException {
session.getConnection().createStatement().execute("drop table address");
session.getConnection().createStatement().execute("drop table person");
session.close();
session.close();
}
}
}