Last active
December 20, 2015 23:18
-
-
Save ricston-git/6211113 to your computer and use it in GitHub Desktop.
MyBatisModule
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
<?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="org.mybatis.sql.AddressMapper"> | |
<select id="selectAddress" parameterType="int" resultType="Address"> | |
SELECT * FROM address | |
WHERE id = #{id} | |
</select> | |
<select id="selectAddressesByPersonId" parameterType="int" resultType="Address"> | |
SELECT * FROM address | |
WHERE personId = #{id} | |
</select> | |
<insert id="insertAddress" parameterType="Address" | |
useGeneratedKeys="true" keyProperty="id"> | |
INSERT INTO address (address, personId) | |
VALUES (#{address}, #{personId}) | |
</insert> | |
<update id="updateAddress" parameterType="Address"> | |
UPDATE address SET | |
address=#{address} | |
WHERE id=#{id} | |
</update> | |
</mapper> |
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
CREATE TABLE person ( | |
id int auto_increment PRIMARY KEY, | |
name varchar(255), | |
surname varchar(255), | |
age int | |
); | |
CREATE TABLE address ( | |
id int auto_increment PRIMARY KEY, | |
address varchar(255), | |
personId int | |
); | |
ALTER TABLE address | |
ADD CONSTRAINT address_person_fk | |
FOREIGN KEY (personId) | |
REFERENCES person (id); |
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
public interface AddressMapper { | |
public Address selectAddress(Integer id); | |
public List<Address> selectAddressesByPersonId(Integer id); | |
public void insertAddress(Address person); | |
public void updateAddress(Address person); | |
} | |
public interface PersonMapper { | |
public Person selectPerson(Integer id); | |
public void insertPerson(Person person); | |
public void updatePerson(Person person); | |
public void deletePerson(Integer id); | |
} |
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
<?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="useGeneratedKeys" value="true" /> | |
</settings> | |
<typeAliases> | |
<typeAlias alias="Person" type="org.mybatis.domain.Person" /> | |
<typeAlias alias="Address" type="org.mybatis.domain.Address" /> | |
</typeAliases> | |
<environments default="development"> | |
<environment id="development"> | |
<transactionManager type="JDBC" /> | |
<dataSource type="POOLED"> | |
<property name="driver" value="org.h2.Driver" /> | |
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" /> | |
<property name="username" value="root" /> | |
<property name="password" value="" /> | |
</dataSource> | |
</environment> | |
</environments> | |
<mappers> | |
<mapper resource="org/mybatis/sql/person-mapper.xml" /> | |
<mapper resource="org/mybatis/sql/address-mapper.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
<mybatis:config name="mybatis" configFile="mybatis-config.xml"/> | |
<sub-flow name="CreateUser"> | |
<set-variable variableName="person" value="#[payload]" /> | |
<mybatis:execute mapper="org.mybatis.sql.PersonMapper" method="insertPerson" /> | |
<foreach collection="#[flowVars.person.addresses]"> | |
<expression-component> | |
payload.personId = flowVars.person.id; | |
</expression-component> | |
<mybatis:execute mapper="org.mybatis.sql.AddressMapper" method="insertAddress" /> | |
</foreach> | |
</sub-flow> |
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
<flow name="CreateUserTransacted"> | |
<mybatis:begin-transaction /> | |
<flow-ref name=”CreateUser” /> | |
<mybatis:commit-transaction /> | |
<rollback-exception-strategy> | |
<mybatis:rollback-transaction /> | |
</rollback-exception-strategy> | |
</flow> |
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
<mybatis:select-one statement="org.mybastis.sql.PersonMapper.selectOne"/> | |
<mybatis:select-list statement="org.mybastis.sql.PersonMapper.selectList"/> | |
<mybatis:select-map statement="org.mybastis.sql.PersonMapper.selectMap" mapKey="id"/> | |
<mybatis:update statement="org.mybastis.sql.PersonMapper.update"/> | |
<mybatis:insert statement="org.mybastis.sql.PersonMapper.selectOne"/> | |
<mybatis:delete statement="org.mybastis.sql.PersonMapper.selectOne"/> |
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
<mybatis:begin-transaction /> | |
<mybatis:commit-transaction /> | |
<mybatis:rollback-transaction /> |
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
<mybatis:config name="mybatis" sqlSessionFactory-ref="sqlSessionFactory" /> | |
<spring:beans> | |
<spring:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> | |
<spring:property name="driverClassName" value="org.h2.Driver" /> | |
<spring:property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" /> | |
<spring:property name="username" value="root" /> | |
<spring:property name="password" value="" /> | |
</spring:bean> | |
<spring:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> | |
<spring:property name="dataSource" ref="dataSource" /> | |
<spring:property name="configLocation" value="classpath:mybatis-config.xml" /> | |
</spring:bean> | |
</spring:beans> |
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
<?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="org.mybatis.sql.PersonMapper"> | |
<resultMap id="personResultMap" type="Person"> | |
<id property="id" column="id" /> | |
<collection property="addresses" column="id" ofType="Address" select="org.mybatis.sql.AddressMapper.selectAddressesByPersonId"/> | |
</resultMap> | |
<select id="selectPerson" parameterType="int" resultMap="personResultMap"> | |
SELECT * FROM person | |
WHERE id = #{id} | |
</select> | |
<insert id="insertPerson" parameterType="Person" useGeneratedKeys="true" keyProperty="id"> | |
INSERT INTO person (name, surname, age) | |
VALUES (#{name}, #{surname}, #{age}) | |
</insert> | |
<update id="updatePerson" parameterType="Person"> | |
UPDATE person SET | |
name=#{name}, | |
surname=#{surname}, | |
age=#{age} | |
WHERE id=#{id} | |
</update> | |
</mapper> |
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
public class Address { | |
private Integer id; | |
private String address; | |
private Integer personId; | |
//getters and setters here | |
} | |
public class Person { | |
private Integer id; | |
private String name; | |
private String surname; | |
private Integer age; | |
private List<Address> addresses; | |
//getters and setters here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment