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,7 +4,6 @@ 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})")
@ -12,11 +11,9 @@ public interface AddressMapper {
public Integer saveAddress(Address address);
@Select("SELECT addressId, streetAddress FROM Address WHERE addressId = #{addressId}")
@Results(value = {
@Result(property = "addressId", column = "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"))
})
@Result(property = "person", column = "personId", javaType = Person.class, one = @One(select = "getPerson")) })
Address getAddresses(Integer addressID);
@Select("SELECT personId FROM address WHERE addressId = #{addressId})")

View File

@ -9,7 +9,6 @@ import org.apache.ibatis.mapping.StatementType;
import java.util.List;
import java.util.Map;
public interface PersonMapper {
@Insert("Insert into person(name) values (#{name})")
@ -25,11 +24,8 @@ public interface PersonMapper {
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"))
@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);
@ -44,7 +40,6 @@ public interface PersonMapper {
@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);

View File

@ -1,6 +1,5 @@
package com.baeldung.mybatis.model;
public class Address {
private Integer addressId;
@ -18,8 +17,6 @@ public class Address {
this.personId = personId;
}
public Address(String streetAddress) {
this.streetAddress = streetAddress;
}

View File

@ -3,7 +3,6 @@ package com.baeldung.mybatis.model;
import java.util.ArrayList;
import java.util.List;
public class Person {
private Integer personId;
@ -30,6 +29,7 @@ public class Person {
public String getName() {
return name;
}
public void addAddress(Address address) {
addresses.add(address);
}

View File

@ -39,10 +39,12 @@ public class MyBatisUtil {
}
public String getPersonByName(String name) {
return new SQL(){{
return new SQL() {
{
SELECT("*");
FROM("person");
WHERE("name like #{name} || '%'");
}}.toString();
}
}.toString();
}
}

View File

@ -30,21 +30,16 @@ public class PersonMapperTest {
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, "
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, "
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);
@ -95,6 +90,7 @@ public class PersonMapperTest {
Person returnedPerson = personMapper.getPerson(id);
assertEquals("Baljeet1", returnedPerson.getName());
}
@Test
public void whenPersoSaved_ThenMapIsReturned() {
Person person = new Person("Baljeet S");