├── elearning
├── README.txt
├── src
│ └── main
│ │ ├── webapp
│ │ ├── index.jsp
│ │ └── WEB-INF
│ │ │ └── web.xml
│ │ └── resources
│ │ └── log4j.properties
└── pom.xml
├── .gitignore
├── README.md
├── chapter01
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── sql
│ │ │ │ ├── drop_tables.sql
│ │ │ │ ├── sample_data.sql
│ │ │ │ └── create_tables.sql
│ │ │ ├── application.properties
│ │ │ ├── log4j.properties
│ │ │ ├── mybatis-config.xml
│ │ │ └── com
│ │ │ │ └── mybatis3
│ │ │ │ └── mappers
│ │ │ │ └── StudentMapper.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── mybatis3
│ │ │ ├── mappers
│ │ │ └── StudentMapper.java
│ │ │ ├── domain
│ │ │ └── Student.java
│ │ │ ├── services
│ │ │ ├── StudentService.java
│ │ │ └── JdbcStudentService.java
│ │ │ └── util
│ │ │ └── MyBatisSqlSessionFactory.java
│ └── test
│ │ ├── resources
│ │ └── log4j.properties
│ │ └── java
│ │ └── com
│ │ └── mybatis3
│ │ └── services
│ │ ├── TestDataPopulator.java
│ │ └── StudentServiceTest.java
├── README.txt
└── pom.xml
├── chapter02
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── sql
│ │ │ │ ├── drop_tables.sql
│ │ │ │ ├── sample_data.sql
│ │ │ │ └── create_tables.sql
│ │ │ ├── application.properties
│ │ │ ├── log4j.properties
│ │ │ ├── com
│ │ │ │ └── mybatis3
│ │ │ │ │ └── mappers
│ │ │ │ │ └── StudentMapper.xml
│ │ │ ├── mybatis-config.xml
│ │ │ └── full-mybatis-config.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── mybatis3
│ │ │ ├── mappers
│ │ │ └── StudentMapper.java
│ │ │ ├── typehandlers
│ │ │ └── PhoneTypeHandler.java
│ │ │ ├── domain
│ │ │ ├── Student.java
│ │ │ └── PhoneNumber.java
│ │ │ ├── util
│ │ │ ├── DataSourceFactory.java
│ │ │ └── MyBatisUtil.java
│ │ │ └── services
│ │ │ └── StudentService.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── mybatis3
│ │ └── services
│ │ ├── TestDataPopulator.java
│ │ └── StudentServiceTest.java
├── README.txt
└── pom.xml
├── chapter03
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── sql
│ │ │ │ ├── drop_tables.sql
│ │ │ │ ├── sample_data.sql
│ │ │ │ └── create_tables.sql
│ │ │ ├── log4j.properties
│ │ │ ├── application.properties
│ │ │ ├── com
│ │ │ │ └── mybatis3
│ │ │ │ │ └── mappers
│ │ │ │ │ ├── AddressMapper.xml
│ │ │ │ │ ├── CourseMapper.xml
│ │ │ │ │ ├── TutorMapper.xml
│ │ │ │ │ └── StudentMapper.xml
│ │ │ └── mybatis-config.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── mybatis3
│ │ │ ├── mappers
│ │ │ ├── AddressMapper.java
│ │ │ ├── TutorMapper.java
│ │ │ ├── CourseMapper.java
│ │ │ └── StudentMapper.java
│ │ │ ├── services
│ │ │ ├── TutorService.java
│ │ │ ├── CourseService.java
│ │ │ └── StudentService.java
│ │ │ ├── typehandlers
│ │ │ └── PhoneTypeHandler.java
│ │ │ ├── domain
│ │ │ ├── Student.java
│ │ │ ├── Tutor.java
│ │ │ ├── PhoneNumber.java
│ │ │ ├── Address.java
│ │ │ └── Course.java
│ │ │ └── util
│ │ │ └── MyBatisUtil.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── mybatis3
│ │ └── services
│ │ ├── TutorServiceTest.java
│ │ ├── TestDataPopulator.java
│ │ ├── CourseServiceTest.java
│ │ └── StudentServiceTest.java
├── README.txt
└── pom.xml
├── chapter04
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── sql
│ │ │ │ ├── drop_tables.sql
│ │ │ │ ├── sample_data.sql
│ │ │ │ └── create_tables.sql
│ │ │ ├── application.properties
│ │ │ ├── log4j.properties
│ │ │ ├── com
│ │ │ │ └── mybatis3
│ │ │ │ │ └── mappers
│ │ │ │ │ ├── StudentMapper.xml
│ │ │ │ │ └── TutorMapper.xml
│ │ │ └── mybatis-config.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── mybatis3
│ │ │ ├── mappers
│ │ │ ├── AddressMapper.java
│ │ │ ├── StudentMapper.java
│ │ │ └── TutorMapper.java
│ │ │ ├── typehandlers
│ │ │ └── PhoneTypeHandler.java
│ │ │ ├── domain
│ │ │ ├── Student.java
│ │ │ ├── Tutor.java
│ │ │ ├── PhoneNumber.java
│ │ │ ├── Address.java
│ │ │ └── Course.java
│ │ │ ├── util
│ │ │ └── MyBatisUtil.java
│ │ │ ├── sqlproviders
│ │ │ └── TutorDynaSqlProvider.java
│ │ │ └── services
│ │ │ ├── TutorService.java
│ │ │ └── StudentService.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── mybatis3
│ │ └── services
│ │ ├── TestDataPopulator.java
│ │ ├── TutorServiceTest.java
│ │ └── StudentServiceTest.java
├── README.txt
└── pom.xml
└── chapter05
├── src
├── main
│ ├── resources
│ │ ├── sql
│ │ │ ├── drop_tables.sql
│ │ │ ├── sample_data.sql
│ │ │ └── create_tables.sql
│ │ ├── application.properties
│ │ ├── log4j.properties
│ │ ├── com
│ │ │ └── mybatis3
│ │ │ │ └── mappers
│ │ │ │ ├── StudentMapper.xml
│ │ │ │ └── TutorMapper.xml
│ │ └── applicationContext.xml
│ └── java
│ │ └── com
│ │ └── mybatis3
│ │ ├── mappers
│ │ ├── AddressMapper.java
│ │ ├── StudentMapper.java
│ │ └── TutorMapper.java
│ │ ├── config
│ │ └── AppConfig.java
│ │ ├── typehandlers
│ │ └── PhoneTypeHandler.java
│ │ ├── domain
│ │ ├── Student.java
│ │ ├── Tutor.java
│ │ ├── PhoneNumber.java
│ │ ├── Address.java
│ │ └── Course.java
│ │ ├── services
│ │ ├── TutorService.java
│ │ └── StudentService.java
│ │ ├── util
│ │ └── MyBatisUtil.java
│ │ └── sqlproviders
│ │ └── TutorDynaSqlProvider.java
└── test
│ └── java
│ └── com
│ └── mybatis3
│ └── services
│ ├── TestDataPopulator.java
│ ├── TutorServiceTest.java
│ └── StudentServiceTest.java
├── README.txt
└── pom.xml
/elearning/README.txt:
--------------------------------------------------------------------------------
1 | This module demonstrate developing a simple web app using MyBatis persistence framework.
--------------------------------------------------------------------------------
/elearning/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World!
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .project
3 | .classpath
4 | .settings
5 | .springBeans
6 |
7 | target
8 | bin
9 | build
10 |
11 | *.class
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Java Persistence with MyBatis3 Book Sample Code
2 | ========================
3 | 1. Getting started with MyBatis
4 | 2. Bootstrapping MyBatis
5 | 3. SQL Mappers using XML
6 | 4. SQL Mappers using Annotations
7 | 5. Integration with Spring
8 |
--------------------------------------------------------------------------------
/chapter01/src/main/resources/sql/drop_tables.sql:
--------------------------------------------------------------------------------
1 |
2 | DROP TABLE IF EXISTS USER_PICS;
3 | DROP TABLE IF EXISTS COURSE_ENROLLMENT;
4 | DROP TABLE IF EXISTS COURSES;
5 | DROP TABLE IF EXISTS TUTORS;
6 | DROP TABLE IF EXISTS STUDENTS;
7 | DROP TABLE IF EXISTS ADDRESSES;
8 |
9 |
--------------------------------------------------------------------------------
/chapter02/src/main/resources/sql/drop_tables.sql:
--------------------------------------------------------------------------------
1 |
2 | DROP TABLE IF EXISTS USER_PICS;
3 | DROP TABLE IF EXISTS COURSE_ENROLLMENT;
4 | DROP TABLE IF EXISTS COURSES;
5 | DROP TABLE IF EXISTS TUTORS;
6 | DROP TABLE IF EXISTS STUDENTS;
7 | DROP TABLE IF EXISTS ADDRESSES;
8 |
9 |
--------------------------------------------------------------------------------
/chapter03/src/main/resources/sql/drop_tables.sql:
--------------------------------------------------------------------------------
1 |
2 | DROP TABLE IF EXISTS USER_PICS;
3 | DROP TABLE IF EXISTS COURSE_ENROLLMENT;
4 | DROP TABLE IF EXISTS COURSES;
5 | DROP TABLE IF EXISTS TUTORS;
6 | DROP TABLE IF EXISTS STUDENTS;
7 | DROP TABLE IF EXISTS ADDRESSES;
8 |
9 |
--------------------------------------------------------------------------------
/chapter04/src/main/resources/sql/drop_tables.sql:
--------------------------------------------------------------------------------
1 |
2 | DROP TABLE IF EXISTS USER_PICS;
3 | DROP TABLE IF EXISTS COURSE_ENROLLMENT;
4 | DROP TABLE IF EXISTS COURSES;
5 | DROP TABLE IF EXISTS TUTORS;
6 | DROP TABLE IF EXISTS STUDENTS;
7 | DROP TABLE IF EXISTS ADDRESSES;
8 |
9 |
--------------------------------------------------------------------------------
/chapter05/src/main/resources/sql/drop_tables.sql:
--------------------------------------------------------------------------------
1 |
2 | DROP TABLE IF EXISTS USER_PICS;
3 | DROP TABLE IF EXISTS COURSE_ENROLLMENT;
4 | DROP TABLE IF EXISTS COURSES;
5 | DROP TABLE IF EXISTS TUTORS;
6 | DROP TABLE IF EXISTS STUDENTS;
7 | DROP TABLE IF EXISTS ADDRESSES;
8 |
9 |
--------------------------------------------------------------------------------
/elearning/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | Archetype Created Web Application
7 |
8 |
--------------------------------------------------------------------------------
/chapter01/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | ################### DataSource Configuration ##########################
4 |
5 | jdbc.driverClassName=com.mysql.jdbc.Driver
6 | jdbc.url=jdbc:mysql://localhost:3306/elearning
7 | jdbc.username=root
8 | jdbc.password=admin
9 |
--------------------------------------------------------------------------------
/chapter02/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | ################### DataSource Configuration ##########################
4 |
5 | jdbc.driverClassName=com.mysql.jdbc.Driver
6 | jdbc.url=jdbc:mysql://localhost:3306/elearning
7 | jdbc.username=root
8 | jdbc.password=admin
9 |
--------------------------------------------------------------------------------
/chapter04/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | ################### DataSource Configuration ##########################
4 |
5 | jdbc.driverClassName=com.mysql.jdbc.Driver
6 | jdbc.url=jdbc:mysql://localhost:3306/elearning
7 | jdbc.username=root
8 | jdbc.password=admin
9 |
--------------------------------------------------------------------------------
/chapter05/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | ################### DataSource Configuration ##########################
4 |
5 | jdbc.driverClassName=com.mysql.jdbc.Driver
6 | jdbc.url=jdbc:mysql://localhost:3306/elearning
7 | jdbc.username=root
8 | jdbc.password=admin
9 |
10 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/mappers/AddressMapper.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.mappers;
2 |
3 | import com.mybatis3.domain.Address;
4 |
5 |
6 |
7 | /**
8 | * @author Siva
9 | *
10 | */
11 | public interface AddressMapper
12 | {
13 | Address findAddressById(Integer id);
14 | }
15 |
--------------------------------------------------------------------------------
/chapter01/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.logger.com.mybatis3=DEBUG
--------------------------------------------------------------------------------
/chapter01/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=DEBUG, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.logger.com.mybatis3=DEBUG
--------------------------------------------------------------------------------
/chapter02/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.logger.com.mybatis3=DEBUG
--------------------------------------------------------------------------------
/chapter03/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.logger.com.mybatis3=DEBUG
--------------------------------------------------------------------------------
/chapter04/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.logger.com.mybatis3=DEBUG
--------------------------------------------------------------------------------
/chapter05/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.logger.com.mybatis3=DEBUG
--------------------------------------------------------------------------------
/chapter03/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | ################### DataSource Configuration ##########################
4 |
5 | jdbc.driverClassName=com.mysql.jdbc.Driver
6 | jdbc.url=jdbc:mysql://localhost:3306/elearning
7 | jdbc.username=root
8 | jdbc.password=admin
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/mappers/TutorMapper.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.mappers;
2 |
3 | import com.mybatis3.domain.Tutor;
4 |
5 |
6 |
7 | /**
8 | * @author Siva
9 | *
10 | */
11 | public interface TutorMapper
12 | {
13 |
14 | Tutor selectTutorWithCourses(int tutorId);
15 |
16 | Tutor selectTutorById(int tutorId);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/chapter01/README.txt:
--------------------------------------------------------------------------------
1 | Chapter 1: Getting started with MyBatis
2 | =======================================
3 | This module, chapter01, is a maven based java project with MyBatis configured.
4 |
5 | How to Run:
6 | 1. Create MySQL Database tables using scripts in src/main/resources/sql folder.
7 | 2. Configure Database Connection properties like hostname, username and password in src/main/resources/application.properties file.
8 | 3. Run StudentServiceTest JUnit Test class.
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/mappers/AddressMapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.mappers;
5 |
6 | import org.apache.ibatis.annotations.Select;
7 |
8 | import com.mybatis3.domain.Address;
9 |
10 | /**
11 | * @author Siva
12 | *
13 | */
14 | public interface AddressMapper
15 | {
16 | @Select("select addr_id as addrId, street, city, state, zip, country from addresses where addr_id=#{id}")
17 | Address selectAddressById(int id);
18 | }
19 |
--------------------------------------------------------------------------------
/chapter01/src/main/java/com/mybatis3/mappers/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.mappers;
2 |
3 | import java.util.List;
4 |
5 | import com.mybatis3.domain.Student;
6 |
7 |
8 | /**
9 | * @author Siva
10 | *
11 | */
12 | public interface StudentMapper
13 | {
14 |
15 | List findAllStudents();
16 |
17 | Student findStudentById(Integer id);
18 |
19 | void insertStudent(Student student);
20 |
21 | void updateStudent(Student student);
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter02/src/main/java/com/mybatis3/mappers/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.mappers;
2 |
3 | import java.util.List;
4 |
5 | import com.mybatis3.domain.Student;
6 |
7 |
8 | /**
9 | * @author Siva
10 | *
11 | */
12 | public interface StudentMapper
13 | {
14 |
15 | List findAllStudents();
16 |
17 | Student findStudentById(Integer id);
18 |
19 | void insertStudent(Student student);
20 |
21 | void updateStudent(Student student);
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/mappers/CourseMapper.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.mappers;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import com.mybatis3.domain.Course;
7 |
8 |
9 |
10 | /**
11 | * @author Siva
12 | *
13 | */
14 | public interface CourseMapper
15 | {
16 |
17 | List selectCoursesByTutor(int tutorId);
18 |
19 | List searchCourses(Map map);
20 |
21 | List searchCoursesByTutors(Map map);
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter03/README.txt:
--------------------------------------------------------------------------------
1 | Chapter 3: SQL Mappers using XML
2 | =================================
3 | This chapter describes mapping SQL statements and query results to java beans in SQL Mappers using XML based approach.
4 | Topics covered:
5 | • CRUD Mapping
6 | • ResultSet Mapping
7 | • One-To-One, One-To-Many mappings
8 | • Dynamic SQL mapping
9 |
10 | How to Run:
11 | 1. Configure Database Connection properties like hostname, username and password in src/main/resources/application.properties file.
12 | 2. Run JUnit Test classes in com.mybatis3.services package under src/test/java/ folder.
13 |
14 |
--------------------------------------------------------------------------------
/elearning/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, stdout
2 |
3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n
6 |
7 | log4j.appender.file=org.apache.log4j.RollingFileAppender
8 | log4j.appender.file.File=mybatisapp.log
9 | log4j.appender.R.MaxFileSize=100KB
10 | # Keep one backup file
11 | log4j.appender.R.MaxBackupIndex=10
12 | log4j.appender.file.layout=org.apache.log4j.PatternLayout
13 | log4j.appender.file.layout.ConversionPattern=%d [%-5p] %c - %m%n
14 |
15 | log4j.logger.chapter02=DEBUG
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/mappers/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.mappers;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import com.mybatis3.domain.Student;
7 |
8 |
9 |
10 | /**
11 | * @author Siva
12 | *
13 | */
14 | public interface StudentMapper
15 | {
16 |
17 | List findAllStudents();
18 |
19 | Student findStudentById(Integer id);
20 |
21 | Student selectStudentWithAddress(int id);
22 |
23 | void insertStudent(Student student);
24 |
25 | void insertStudentWithMap(Map map);
26 |
27 | void updateStudent(Student student);
28 |
29 | int deleteStudent(int id);
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/mappers/AddressMapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.mappers;
5 |
6 | import org.apache.ibatis.annotations.Insert;
7 | import org.apache.ibatis.annotations.Select;
8 |
9 | import com.mybatis3.domain.Address;
10 |
11 | /**
12 | * @author Siva
13 | *
14 | */
15 | public interface AddressMapper
16 | {
17 | @Select("select addr_id as addrId, street, city, state, zip, country from addresses where addr_id=#{id}")
18 | Address selectAddressById(int id);
19 |
20 | @Insert("insert into addresses(street, city, state, zip, country) values(#{street},#{city},#{state},#{zip},#{country})")
21 | int insertAddress(Address address);
22 | }
23 |
--------------------------------------------------------------------------------
/chapter02/README.txt:
--------------------------------------------------------------------------------
1 | Chapter 2: Bootstrapping MyBatis
2 | =======================================
3 | This module, chapter02, is a maven based java project to demonstrate the following approaches to configure and bootstrap MyBatis.
4 | . Configuration using XML
5 | . Configuration using Java API.
6 |
7 | Note: You can create MySQL Database tables using scripts in src/main/resources/sql folder.
8 |
9 | How to Run:
10 | 1. Configure Database Connection properties like hostname, username and password in src/main/resources/application.properties file.
11 | 2. Run StudentServiceTest JUnit Test class by using the appropriate configuration style in com.mybatis3.services.StudentServiceTest.setup() method.
12 |
--------------------------------------------------------------------------------
/chapter05/README.txt:
--------------------------------------------------------------------------------
1 | Chapter 5: Integration with Spring
2 | This chapter explains how to integration MyBatis with Spring framework.
3 | Topics covered:
4 | . Configuring MyBatis in Spring ApplicationContext
5 | . Injecting SqlSession and SQL Mappers
6 | . Transaction Management using Spring
7 |
8 | How to Run:
9 | Update the database properties in application.properties file.
10 | You can run the JUnit tests in src/test/java folder.
11 | In JUnit Tests we have Database Initialization logic to setup sample data.
12 |
13 | public class StudentServiceTest
14 | {
15 | @BeforeClass
16 | public static void setup() {
17 | studentService = new StudentService();
18 | TestDataPopulator.initDatabase(); // this will drop and re-create database and populates sample data.
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/services/TutorService.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import org.apache.ibatis.session.SqlSession;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import com.mybatis3.domain.Tutor;
8 | import com.mybatis3.mappers.TutorMapper;
9 | import com.mybatis3.util.MyBatisUtil;
10 |
11 |
12 | public class TutorService
13 | {
14 | private Logger logger = LoggerFactory.getLogger(getClass());
15 |
16 |
17 | public Tutor findTutorById(int tutorId) {
18 | logger.debug("findTutorById :"+tutorId);
19 | SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
20 | try {
21 | TutorMapper mapper = sqlSession.getMapper(TutorMapper.class);
22 | return mapper.selectTutorById(tutorId);
23 | }
24 |
25 | finally {
26 | sqlSession.close();
27 | }
28 | }
29 |
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/chapter03/src/main/resources/com/mybatis3/mappers/AddressMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
20 |
21 |
--------------------------------------------------------------------------------
/chapter04/README.txt:
--------------------------------------------------------------------------------
1 | Chapter 4: SQL Mappers using Annotations
2 | ========================================
3 | This chapter describes mapping SQL statements and query results to java beans in SQL Mappers using Annotation based approach.
4 | Topics covered:
5 | • CRUD Mapping
6 | • ResultSet Mapping
7 | • One-To-One, One-To-Many mappings
8 | • Dynamic SQL mapping
9 |
10 | How to Run:
11 | Update the database properties in application.properties file.
12 | You can run the JUnit tests in src/test/java folder.
13 | In JUnit Tests we have Database Initialization logic to setup sample data.
14 |
15 | public class StudentServiceTest
16 | {
17 | @BeforeClass
18 | public static void setup() {
19 | studentService = new StudentService();
20 | TestDataPopulator.initDatabase(); // this will drop and re-create database and populates sample data.
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter04/src/main/resources/com/mybatis3/mappers/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/chapter05/src/main/resources/com/mybatis3/mappers/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/chapter01/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/chapter03/src/test/java/com/mybatis3/services/TutorServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import java.util.List;
4 |
5 | import org.junit.AfterClass;
6 | import org.junit.Assert;
7 | import org.junit.BeforeClass;
8 | import org.junit.Test;
9 |
10 | import com.mybatis3.domain.Course;
11 | import com.mybatis3.domain.Tutor;
12 |
13 |
14 |
15 | public class TutorServiceTest
16 | {
17 | private static TutorService tutorService;
18 |
19 | @BeforeClass
20 | public static void setup()
21 | {
22 | TestDataPopulator.initDatabase();
23 | tutorService = new TutorService();
24 | }
25 |
26 | @AfterClass
27 | public static void teardown()
28 | {
29 | tutorService = null;
30 | }
31 |
32 |
33 | @Test
34 | public void testFindTutorById() {
35 | Tutor tutor = tutorService.findTutorById(1);
36 | Assert.assertNotNull(tutor);
37 | List courses = tutor.getCourses();
38 | Assert.assertNotNull(courses);
39 | for (Course course : courses)
40 | {
41 | Assert.assertNotNull(course);
42 | System.out.println(course);
43 | }
44 | }
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/chapter03/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/config/AppConfig.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.config;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.context.annotation.Configuration;
5 | /*
6 | import javax.sql.DataSource;
7 | import org.apache.ibatis.datasource.pooled.PooledDataSource;
8 | import org.apache.ibatis.session.SqlSessionFactory;
9 | import org.mybatis.spring.SqlSessionFactoryBean;
10 | import org.springframework.context.annotation.Bean;
11 | */
12 | /**
13 | * @author Siva
14 | *
15 | */
16 | @Configuration
17 | @MapperScan(value="com.mybatis3.mappers")
18 | public class AppConfig
19 | {
20 | /*
21 | @Bean
22 | public DataSource dataSource() {
23 | return new PooledDataSource("com.mysql.jdbc.Driver",
24 | "jdbc:mysql://localhost:3306/elearning",
25 | "root", "admin");
26 | }
27 |
28 | @Bean
29 | public SqlSessionFactory sqlSessionFactory() throws Exception {
30 | SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
31 | sessionFactory.setDataSource(dataSource());
32 | return sessionFactory.getObject();
33 | }
34 | */
35 | }
36 |
--------------------------------------------------------------------------------
/chapter04/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/chapter01/src/main/resources/com/mybatis3/mappers/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
18 |
19 |
22 |
23 |
24 | INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB) VALUES(#{studId},#{name},#{email},#{dob})
25 |
26 |
27 |
28 | UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{studId}
29 |
30 |
31 |
--------------------------------------------------------------------------------
/chapter02/src/main/resources/com/mybatis3/mappers/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
18 |
19 |
22 |
23 |
24 | INSERT INTO STUDENTS(NAME,EMAIL,DOB) VALUES(#{name},#{email},#{dob})
25 |
26 |
27 |
28 | UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{studId}
29 |
30 |
31 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/services/CourseService.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import org.apache.ibatis.session.SqlSession;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 |
10 | import com.mybatis3.domain.Course;
11 | import com.mybatis3.mappers.CourseMapper;
12 | import com.mybatis3.util.MyBatisUtil;
13 |
14 |
15 | public class CourseService
16 | {
17 | private Logger logger = LoggerFactory.getLogger(getClass());
18 |
19 | public List searchCourses(Map map)
20 | {
21 | logger.debug("searchCourses By :"+map);
22 | SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
23 | try {
24 | CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
25 | return mapper.searchCourses(map);
26 | }
27 |
28 | finally {
29 | sqlSession.close();
30 | }
31 | }
32 |
33 | public List searchCoursesByTutors(Map map) {
34 | SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
35 | try {
36 | CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
37 | return mapper.searchCoursesByTutors(map);
38 | }
39 |
40 | finally {
41 | sqlSession.close();
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/chapter02/src/main/java/com/mybatis3/typehandlers/PhoneTypeHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.typehandlers;
5 |
6 | import java.sql.CallableStatement;
7 | import java.sql.PreparedStatement;
8 | import java.sql.ResultSet;
9 | import java.sql.SQLException;
10 |
11 | import org.apache.ibatis.type.BaseTypeHandler;
12 | import org.apache.ibatis.type.JdbcType;
13 |
14 | import com.mybatis3.domain.PhoneNumber;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | public class PhoneTypeHandler extends BaseTypeHandler{
22 |
23 | @Override
24 | public void setNonNullParameter(PreparedStatement ps, int i,
25 | PhoneNumber parameter, JdbcType jdbcType) throws SQLException {
26 | ps.setString(i, parameter.getAsString());
27 | }
28 |
29 | @Override
30 | public PhoneNumber getNullableResult(ResultSet rs, String columnName)
31 | throws SQLException {
32 | return new PhoneNumber(rs.getString(columnName));
33 | }
34 |
35 | @Override
36 | public PhoneNumber getNullableResult(ResultSet rs, int columnIndex)
37 | throws SQLException {
38 | return new PhoneNumber(rs.getString(columnIndex));
39 | }
40 |
41 | @Override
42 | public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex)
43 | throws SQLException {
44 | return new PhoneNumber(cs.getString(columnIndex));
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/typehandlers/PhoneTypeHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.typehandlers;
5 |
6 | import java.sql.CallableStatement;
7 | import java.sql.PreparedStatement;
8 | import java.sql.ResultSet;
9 | import java.sql.SQLException;
10 |
11 | import org.apache.ibatis.type.BaseTypeHandler;
12 | import org.apache.ibatis.type.JdbcType;
13 |
14 | import com.mybatis3.domain.PhoneNumber;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | public class PhoneTypeHandler extends BaseTypeHandler{
22 |
23 | @Override
24 | public void setNonNullParameter(PreparedStatement ps, int i,
25 | PhoneNumber parameter, JdbcType jdbcType) throws SQLException {
26 | ps.setString(i, parameter.getAsString());
27 | }
28 |
29 | @Override
30 | public PhoneNumber getNullableResult(ResultSet rs, String columnName)
31 | throws SQLException {
32 | return new PhoneNumber(rs.getString(columnName));
33 | }
34 |
35 | @Override
36 | public PhoneNumber getNullableResult(ResultSet rs, int columnIndex)
37 | throws SQLException {
38 | return new PhoneNumber(rs.getString(columnIndex));
39 | }
40 |
41 | @Override
42 | public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex)
43 | throws SQLException {
44 | return new PhoneNumber(cs.getString(columnIndex));
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/typehandlers/PhoneTypeHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.typehandlers;
5 |
6 | import java.sql.CallableStatement;
7 | import java.sql.PreparedStatement;
8 | import java.sql.ResultSet;
9 | import java.sql.SQLException;
10 |
11 | import org.apache.ibatis.type.BaseTypeHandler;
12 | import org.apache.ibatis.type.JdbcType;
13 |
14 | import com.mybatis3.domain.PhoneNumber;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | public class PhoneTypeHandler extends BaseTypeHandler{
22 |
23 | @Override
24 | public void setNonNullParameter(PreparedStatement ps, int i,
25 | PhoneNumber parameter, JdbcType jdbcType) throws SQLException {
26 | ps.setString(i, parameter.getAsString());
27 | }
28 |
29 | @Override
30 | public PhoneNumber getNullableResult(ResultSet rs, String columnName)
31 | throws SQLException {
32 | return new PhoneNumber(rs.getString(columnName));
33 | }
34 |
35 | @Override
36 | public PhoneNumber getNullableResult(ResultSet rs, int columnIndex)
37 | throws SQLException {
38 | return new PhoneNumber(rs.getString(columnIndex));
39 | }
40 |
41 | @Override
42 | public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex)
43 | throws SQLException {
44 | return new PhoneNumber(cs.getString(columnIndex));
45 | }
46 |
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/typehandlers/PhoneTypeHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.typehandlers;
5 |
6 | import java.sql.CallableStatement;
7 | import java.sql.PreparedStatement;
8 | import java.sql.ResultSet;
9 | import java.sql.SQLException;
10 |
11 | import org.apache.ibatis.type.BaseTypeHandler;
12 | import org.apache.ibatis.type.JdbcType;
13 |
14 | import com.mybatis3.domain.PhoneNumber;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | public class PhoneTypeHandler extends BaseTypeHandler{
22 |
23 | @Override
24 | public void setNonNullParameter(PreparedStatement ps, int i,
25 | PhoneNumber parameter, JdbcType jdbcType) throws SQLException {
26 | ps.setString(i, parameter.getAsString());
27 | }
28 |
29 | @Override
30 | public PhoneNumber getNullableResult(ResultSet rs, String columnName)
31 | throws SQLException {
32 | return new PhoneNumber(rs.getString(columnName));
33 | }
34 |
35 | @Override
36 | public PhoneNumber getNullableResult(ResultSet rs, int columnIndex)
37 | throws SQLException {
38 | return new PhoneNumber(rs.getString(columnIndex));
39 | }
40 |
41 | @Override
42 | public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex)
43 | throws SQLException {
44 | return new PhoneNumber(cs.getString(columnIndex));
45 | }
46 |
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/chapter01/src/main/java/com/mybatis3/domain/Student.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.util.Date;
4 |
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Student
11 | {
12 | private Integer studId;
13 | private String name;
14 | private String email;
15 | private Date dob;
16 |
17 | public Student() {
18 |
19 | }
20 |
21 | public Student(Integer studId) {
22 | this.studId = studId;
23 | }
24 |
25 | public Student(Integer studId, String name, String email, Date dob) {
26 | this.studId = studId;
27 | this.name = name;
28 | this.email = email;
29 | this.dob = dob;
30 | }
31 |
32 | @Override
33 | public String toString() {
34 | return "Student [studId=" + studId + ", name=" + name + ", email="
35 | + email + ", dob=" + dob + "]";
36 | }
37 |
38 | public Integer getStudId() {
39 | return studId;
40 | }
41 | public void setStudId(Integer studId) {
42 | this.studId = studId;
43 | }
44 | public String getName() {
45 | return name;
46 | }
47 | public void setName(String name) {
48 | this.name = name;
49 | }
50 | public String getEmail() {
51 | return email;
52 | }
53 | public void setEmail(String email) {
54 | this.email = email;
55 | }
56 | public Date getDob() {
57 | return dob;
58 | }
59 | public void setDob(Date dob) {
60 | this.dob = dob;
61 | }
62 |
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/chapter04/src/main/resources/com/mybatis3/mappers/TutorMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/chapter05/src/main/resources/com/mybatis3/mappers/TutorMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/chapter02/src/main/java/com/mybatis3/domain/Student.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.util.Date;
4 |
5 | import org.apache.ibatis.type.Alias;
6 |
7 |
8 | /**
9 | * @author Siva
10 | *
11 | */
12 | @Alias("Student")
13 | public class Student
14 | {
15 | private Integer studId;
16 | private String name;
17 | private String email;
18 | private Date dob;
19 |
20 | public Student() {
21 |
22 | }
23 |
24 | public Student(Integer studId) {
25 | this.studId = studId;
26 | }
27 |
28 | public Student(Integer studId, String name, String email, Date dob) {
29 | this.studId = studId;
30 | this.name = name;
31 | this.email = email;
32 | this.dob = dob;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "Student [studId=" + studId + ", name=" + name + ", email="
38 | + email + ", dob=" + dob + "]";
39 | }
40 |
41 | public Integer getStudId() {
42 | return studId;
43 | }
44 | public void setStudId(Integer studId) {
45 | this.studId = studId;
46 | }
47 | public String getName() {
48 | return name;
49 | }
50 | public void setName(String name) {
51 | this.name = name;
52 | }
53 | public String getEmail() {
54 | return email;
55 | }
56 | public void setEmail(String email) {
57 | this.email = email;
58 | }
59 | public Date getDob() {
60 | return dob;
61 | }
62 | public void setDob(Date dob) {
63 | this.dob = dob;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/chapter01/src/main/resources/sql/sample_data.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | --Sample data for table ADDRESSES
4 |
5 | INSERT INTO ADDRESSES (ADDR_ID,STREET,CITY,STATE,ZIP,COUNTRY) VALUES
6 | (1,'4891 Pacific Hwy','San Diego','CA','92110','San Diego'),
7 | (2,'2400 N Jefferson St','Perry','FL','32347','Taylor'),
8 | (3,'710 N Cable Rd','Lima','OH','45825','Allen'),
9 | (4,'5108 W Gore Blvd','Lawton','OK','32365','Comanche');
10 |
11 | -- Sample data for table STUDENTS
12 |
13 | INSERT INTO STUDENTS (STUD_ID,NAME,EMAIL,PHONE,DOB,BIO,PIC,ADDR_ID) VALUES
14 | (1,'Timothy','timothy@gmail.com','123-123-1234','1988-04-25',NULL,NULL,3),
15 | (2,'Douglas','douglas@gmail.com','789-456-1234','1990-08-15',NULL,NULL,4);
16 |
17 | -- Sample data for table TUTORS
18 |
19 | INSERT INTO TUTORS (TUTOR_ID,NAME,EMAIL,PHONE,DOB,BIO,PIC,ADDR_ID) VALUES
20 | (1,'John','john@gmail.com','111-222-3333','1980-05-20',NULL,NULL,1),
21 | (2,'Paul','paul@gmail.com','123-321-4444','1981-03-15',NULL,NULL,2);
22 |
23 | -- Sample data for table courses
24 |
25 | INSERT INTO COURSES (COURSE_ID,NAME,DESCRIPTION,START_DATE,END_DATE,TUTOR_ID) VALUES
26 | (1,'Quickstart Core Java','Core Java Programming','2013-03-01','2013-04-15',1),
27 | (2,'Quickstart JavaEE6','Enterprise App Development using JavaEE6','2013-04-01','2013-08-30',1),
28 | (3,'MyBatis3 Premier','MyBatis 3 framework','2013-06-01','2013-07-15',2);
29 |
30 | -- Sample data for table COURSE_ENROLLMENT
31 |
32 | INSERT INTO COURSE_ENROLLMENT (COURSE_ID,STUD_ID) VALUES
33 | (1,1),
34 | (1,2),
35 | (2,2);
36 |
37 |
--------------------------------------------------------------------------------
/chapter02/src/main/resources/sql/sample_data.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | --Sample data for table ADDRESSES
4 |
5 | INSERT INTO ADDRESSES (ADDR_ID,STREET,CITY,STATE,ZIP,COUNTRY) VALUES
6 | (1,'4891 Pacific Hwy','San Diego','CA','92110','San Diego'),
7 | (2,'2400 N Jefferson St','Perry','FL','32347','Taylor'),
8 | (3,'710 N Cable Rd','Lima','OH','45825','Allen'),
9 | (4,'5108 W Gore Blvd','Lawton','OK','32365','Comanche');
10 |
11 | -- Sample data for table STUDENTS
12 |
13 | INSERT INTO STUDENTS (STUD_ID,NAME,EMAIL,PHONE,DOB,BIO,PIC,ADDR_ID) VALUES
14 | (1,'Timothy','timothy@gmail.com','123-123-1234','1988-04-25',NULL,NULL,3),
15 | (2,'Douglas','douglas@gmail.com','789-456-1234','1990-08-15',NULL,NULL,4);
16 |
17 | -- Sample data for table TUTORS
18 |
19 | INSERT INTO TUTORS (TUTOR_ID,NAME,EMAIL,PHONE,DOB,BIO,PIC,ADDR_ID) VALUES
20 | (1,'John','john@gmail.com','111-222-3333','1980-05-20',NULL,NULL,1),
21 | (2,'Paul','paul@gmail.com','123-321-4444','1981-03-15',NULL,NULL,2);
22 |
23 | -- Sample data for table courses
24 |
25 | INSERT INTO COURSES (COURSE_ID,NAME,DESCRIPTION,START_DATE,END_DATE,TUTOR_ID) VALUES
26 | (1,'Quickstart Core Java','Core Java Programming','2013-03-01','2013-04-15',1),
27 | (2,'Quickstart JavaEE6','Enterprise App Development using JavaEE6','2013-04-01','2013-08-30',1),
28 | (3,'MyBatis3 Premier','MyBatis 3 framework','2013-06-01','2013-07-15',2);
29 |
30 | -- Sample data for table COURSE_ENROLLMENT
31 |
32 | INSERT INTO COURSE_ENROLLMENT (COURSE_ID,STUD_ID) VALUES
33 | (1,1),
34 | (1,2),
35 | (2,2);
36 |
37 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/domain/Student.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 |
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Student implements Serializable
11 | {
12 | private static final long serialVersionUID = 1L;
13 | private Integer studId;
14 | private String name;
15 | private String email;
16 | private PhoneNumber phone;
17 | private Address address;
18 |
19 | @Override
20 | public String toString() {
21 | return "Student [studId=" + studId + ", name=" + name + ", email=" + email
22 | + ", phone=" + (phone==null?null:phone.getAsString()) + ", address=" + address + "]";
23 | }
24 | public Student()
25 | {
26 | }
27 | public Student(Integer id)
28 | {
29 | this.studId = id;
30 | }
31 | public Integer getStudId()
32 | {
33 | return studId;
34 | }
35 | public void setStudId(Integer id)
36 | {
37 | this.studId = id;
38 | }
39 | public String getName()
40 | {
41 | return name;
42 | }
43 | public void setName(String name)
44 | {
45 | this.name = name;
46 | }
47 | public String getEmail()
48 | {
49 | return email;
50 | }
51 | public void setEmail(String email)
52 | {
53 | this.email = email;
54 | }
55 | public Address getAddress() {
56 | return address;
57 | }
58 | public void setAddress(Address address) {
59 | this.address = address;
60 | }
61 | public PhoneNumber getPhone() {
62 | return phone;
63 | }
64 | public void setPhone(PhoneNumber phone) {
65 | this.phone = phone;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/domain/Student.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 |
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Student implements Serializable
11 | {
12 | private static final long serialVersionUID = 1L;
13 | private Integer studId;
14 | private String name;
15 | private String email;
16 | private PhoneNumber phone;
17 | private Address address;
18 |
19 | @Override
20 | public String toString() {
21 | return "Student [studId=" + studId + ", name=" + name + ", email=" + email
22 | + ", phone=" + (phone==null?null:phone.getAsString()) + ", address=" + address + "]";
23 | }
24 | public Student()
25 | {
26 | }
27 | public Student(Integer id)
28 | {
29 | this.studId = id;
30 | }
31 | public Integer getStudId()
32 | {
33 | return studId;
34 | }
35 | public void setStudId(Integer id)
36 | {
37 | this.studId = id;
38 | }
39 | public String getName()
40 | {
41 | return name;
42 | }
43 | public void setName(String name)
44 | {
45 | this.name = name;
46 | }
47 | public String getEmail()
48 | {
49 | return email;
50 | }
51 | public void setEmail(String email)
52 | {
53 | this.email = email;
54 | }
55 | public Address getAddress() {
56 | return address;
57 | }
58 | public void setAddress(Address address) {
59 | this.address = address;
60 | }
61 | public PhoneNumber getPhone() {
62 | return phone;
63 | }
64 | public void setPhone(PhoneNumber phone) {
65 | this.phone = phone;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/domain/Student.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 |
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Student implements Serializable
11 | {
12 | private static final long serialVersionUID = 1L;
13 | private Integer studId;
14 | private String name;
15 | private String email;
16 | private PhoneNumber phone;
17 | private Address address;
18 |
19 | @Override
20 | public String toString() {
21 | return "Student [studId=" + studId + ", name=" + name + ", email=" + email
22 | + ", phone=" + (phone==null?null:phone.getAsString()) + ", address=" + address + "]";
23 | }
24 | public Student()
25 | {
26 | }
27 | public Student(Integer id)
28 | {
29 | this.studId = id;
30 | }
31 | public Integer getStudId()
32 | {
33 | return studId;
34 | }
35 | public void setStudId(Integer id)
36 | {
37 | this.studId = id;
38 | }
39 | public String getName()
40 | {
41 | return name;
42 | }
43 | public void setName(String name)
44 | {
45 | this.name = name;
46 | }
47 | public String getEmail()
48 | {
49 | return email;
50 | }
51 | public void setEmail(String email)
52 | {
53 | this.email = email;
54 | }
55 | public Address getAddress() {
56 | return address;
57 | }
58 | public void setAddress(Address address) {
59 | this.address = address;
60 | }
61 | public PhoneNumber getPhone() {
62 | return phone;
63 | }
64 | public void setPhone(PhoneNumber phone) {
65 | this.phone = phone;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/domain/Tutor.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Tutor implements Serializable
11 | {
12 | private static final long serialVersionUID = 1L;
13 |
14 | private Integer tutorId;
15 | private String name;
16 | private String email;
17 | private Address address;
18 | private List courses;
19 |
20 | @Override
21 | public String toString() {
22 | return "Tutor [tutorId=" + tutorId + ", name=" + name + ", email=" + email
23 | + ", address=" + address + ", courses=" + courses + "]";
24 | }
25 | public Tutor()
26 | {
27 | }
28 | public Tutor(Integer id)
29 | {
30 | this.tutorId = id;
31 | }
32 | public Integer getTutorId()
33 | {
34 | return tutorId;
35 | }
36 | public void setTutorId(Integer id)
37 | {
38 | this.tutorId = id;
39 | }
40 | public String getName()
41 | {
42 | return name;
43 | }
44 | public void setName(String name)
45 | {
46 | this.name = name;
47 | }
48 | public String getEmail()
49 | {
50 | return email;
51 | }
52 | public void setEmail(String email)
53 | {
54 | this.email = email;
55 | }
56 | public Address getAddress()
57 | {
58 | return address;
59 | }
60 | public void setAddress(Address address)
61 | {
62 | this.address = address;
63 | }
64 | public List getCourses() {
65 | return courses;
66 | }
67 | public void setCourses(List courses) {
68 | this.courses = courses;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/domain/Tutor.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Tutor implements Serializable
11 | {
12 | private static final long serialVersionUID = 1L;
13 |
14 | private Integer tutorId;
15 | private String name;
16 | private String email;
17 | private Address address;
18 | private List courses;
19 |
20 | @Override
21 | public String toString() {
22 | return "Tutor [tutorId=" + tutorId + ", name=" + name + ", email=" + email
23 | + ", address=" + address + ", courses=" + courses + "]";
24 | }
25 | public Tutor()
26 | {
27 | }
28 | public Tutor(Integer id)
29 | {
30 | this.tutorId = id;
31 | }
32 | public Integer getTutorId()
33 | {
34 | return tutorId;
35 | }
36 | public void setTutorId(Integer id)
37 | {
38 | this.tutorId = id;
39 | }
40 | public String getName()
41 | {
42 | return name;
43 | }
44 | public void setName(String name)
45 | {
46 | this.name = name;
47 | }
48 | public String getEmail()
49 | {
50 | return email;
51 | }
52 | public void setEmail(String email)
53 | {
54 | this.email = email;
55 | }
56 | public Address getAddress()
57 | {
58 | return address;
59 | }
60 | public void setAddress(Address address)
61 | {
62 | this.address = address;
63 | }
64 | public List getCourses() {
65 | return courses;
66 | }
67 | public void setCourses(List courses) {
68 | this.courses = courses;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/domain/Tutor.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * @author Siva
8 | *
9 | */
10 | public class Tutor implements Serializable
11 | {
12 | private static final long serialVersionUID = 1L;
13 |
14 | private Integer tutorId;
15 | private String name;
16 | private String email;
17 | private Address address;
18 | private List courses;
19 |
20 | @Override
21 | public String toString() {
22 | return "Tutor [tutorId=" + tutorId + ", name=" + name + ", email=" + email
23 | + ", address=" + address + ", courses=" + courses + "]";
24 | }
25 | public Tutor()
26 | {
27 | }
28 | public Tutor(Integer id)
29 | {
30 | this.tutorId = id;
31 | }
32 | public Integer getTutorId()
33 | {
34 | return tutorId;
35 | }
36 | public void setTutorId(Integer id)
37 | {
38 | this.tutorId = id;
39 | }
40 | public String getName()
41 | {
42 | return name;
43 | }
44 | public void setName(String name)
45 | {
46 | this.name = name;
47 | }
48 | public String getEmail()
49 | {
50 | return email;
51 | }
52 | public void setEmail(String email)
53 | {
54 | this.email = email;
55 | }
56 | public Address getAddress()
57 | {
58 | return address;
59 | }
60 | public void setAddress(Address address)
61 | {
62 | this.address = address;
63 | }
64 | public List getCourses() {
65 | return courses;
66 | }
67 | public void setCourses(List courses) {
68 | this.courses = courses;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/chapter02/src/main/java/com/mybatis3/domain/PhoneNumber.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.domain;
5 |
6 |
7 | /**
8 | * @author Siva
9 | *
10 | */
11 | public class PhoneNumber
12 | {
13 | private String countryCode;
14 | private String stateCode;
15 | private String number;
16 |
17 | public PhoneNumber() {
18 | }
19 |
20 | public PhoneNumber(String countryCode, String stateCode, String number) {
21 | super();
22 | this.countryCode = countryCode;
23 | this.stateCode = stateCode;
24 | this.number = number;
25 | }
26 |
27 | public PhoneNumber(String string) {
28 | if(string != null){
29 | String[] parts = string.split("-");
30 | if(parts.length>0) this.countryCode=parts[0];
31 | if(parts.length>1) this.stateCode=parts[1];
32 | if(parts.length>2) this.number=parts[2];
33 |
34 | }
35 | }
36 |
37 | @Override
38 | public String toString() {
39 | return this.getAsString();
40 | }
41 |
42 | public String getCountryCode() {
43 | return countryCode;
44 | }
45 |
46 | public void setCountryCode(String countryCode) {
47 | this.countryCode = countryCode;
48 | }
49 |
50 | public String getStateCode() {
51 | return stateCode;
52 | }
53 |
54 | public void setStateCode(String stateCode) {
55 | this.stateCode = stateCode;
56 | }
57 |
58 | public String getNumber() {
59 | return number;
60 | }
61 |
62 | public void setNumber(String number) {
63 | this.number = number;
64 | }
65 |
66 | public String getAsString() {
67 | return countryCode+"-"+stateCode+"-"+number;
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/chapter04/src/test/java/com/mybatis3/services/TestDataPopulator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.services;
5 |
6 | import java.io.Reader;
7 | import java.sql.Connection;
8 |
9 | import org.apache.ibatis.io.Resources;
10 | import org.apache.ibatis.jdbc.ScriptRunner;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.mybatis3.util.MyBatisUtil;
15 |
16 | /**
17 | * @author Siva
18 | *
19 | */
20 | public class TestDataPopulator
21 | {
22 | private static Logger logger = LoggerFactory.getLogger(TestDataPopulator.class);
23 |
24 | public static void main(String[] args) {
25 | initDatabase();
26 | }
27 |
28 | public static void initDatabase()
29 | {
30 | Connection connection = null;
31 | Reader reader = null;
32 | try {
33 | connection = MyBatisUtil.getConnection();
34 | ScriptRunner scriptRunner = new ScriptRunner(connection);
35 | reader = Resources.getResourceAsReader("sql/drop_tables.sql");
36 | scriptRunner.runScript(reader);
37 | logger.info("drop_tables.sql executed successfully");
38 | reader = Resources.getResourceAsReader("sql/create_tables.sql");
39 | scriptRunner.runScript(reader );
40 | logger.info("create_tables.sql executed successfully");
41 | reader = Resources.getResourceAsReader("sql/sample_data.sql");
42 | scriptRunner.runScript(reader );
43 | logger.info("sample_data.sql executed successfully");
44 | connection.commit();
45 | reader.close();
46 | scriptRunner.closeConnection();
47 | } catch (Exception e) {
48 | throw new RuntimeException(e);
49 | }
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/chapter05/src/test/java/com/mybatis3/services/TestDataPopulator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.services;
5 |
6 | import java.io.Reader;
7 | import java.sql.Connection;
8 |
9 | import org.apache.ibatis.io.Resources;
10 | import org.apache.ibatis.jdbc.ScriptRunner;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.mybatis3.util.MyBatisUtil;
15 |
16 | /**
17 | * @author Siva
18 | *
19 | */
20 | public class TestDataPopulator
21 | {
22 | private static Logger logger = LoggerFactory.getLogger(TestDataPopulator.class);
23 |
24 | public static void main(String[] args) {
25 | initDatabase();
26 | }
27 |
28 | public static void initDatabase()
29 | {
30 | Connection connection = null;
31 | Reader reader = null;
32 | try {
33 | connection = MyBatisUtil.getConnection();
34 | ScriptRunner scriptRunner = new ScriptRunner(connection);
35 | reader = Resources.getResourceAsReader("sql/drop_tables.sql");
36 | scriptRunner.runScript(reader);
37 | logger.info("drop_tables.sql executed successfully");
38 | reader = Resources.getResourceAsReader("sql/create_tables.sql");
39 | scriptRunner.runScript(reader );
40 | logger.info("create_tables.sql executed successfully");
41 | reader = Resources.getResourceAsReader("sql/sample_data.sql");
42 | scriptRunner.runScript(reader );
43 | logger.info("sample_data.sql executed successfully");
44 | connection.commit();
45 | reader.close();
46 | scriptRunner.closeConnection();
47 | } catch (Exception e) {
48 | throw new RuntimeException(e);
49 | }
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/chapter03/src/test/java/com/mybatis3/services/TestDataPopulator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.services;
5 |
6 | import java.io.Reader;
7 | import java.sql.Connection;
8 |
9 | import org.apache.ibatis.io.Resources;
10 | import org.apache.ibatis.jdbc.ScriptRunner;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.mybatis3.util.MyBatisUtil;;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | public class TestDataPopulator
22 | {
23 | private static Logger logger = LoggerFactory.getLogger(TestDataPopulator.class);
24 |
25 | public static void main(String[] args) {
26 | initDatabase();
27 | }
28 |
29 | public static void initDatabase()
30 | {
31 | Connection connection = null;
32 | Reader reader = null;
33 | try {
34 | connection = MyBatisUtil.getConnection();
35 | ScriptRunner scriptRunner = new ScriptRunner(connection);
36 | reader = Resources.getResourceAsReader("sql/drop_tables.sql");
37 | scriptRunner.runScript(reader);
38 | logger.info("drop_tables.sql executed successfully");
39 | reader = Resources.getResourceAsReader("sql/create_tables.sql");
40 | scriptRunner.runScript(reader );
41 | logger.info("create_tables.sql executed successfully");
42 | reader = Resources.getResourceAsReader("sql/sample_data.sql");
43 | scriptRunner.runScript(reader );
44 | logger.info("sample_data.sql executed successfully");
45 | connection.commit();
46 | reader.close();
47 | scriptRunner.closeConnection();
48 | } catch (Exception e) {
49 | throw new RuntimeException(e);
50 | }
51 |
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/chapter01/src/test/java/com/mybatis3/services/TestDataPopulator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.services;
5 |
6 | import java.io.Reader;
7 | import java.sql.Connection;
8 |
9 | import org.apache.ibatis.io.Resources;
10 | import org.apache.ibatis.jdbc.ScriptRunner;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.mybatis3.util.MyBatisSqlSessionFactory;
15 |
16 | /**
17 | * @author Siva
18 | *
19 | */
20 | public class TestDataPopulator
21 | {
22 | private static Logger logger = LoggerFactory.getLogger(TestDataPopulator.class);
23 |
24 | public static void main(String[] args) {
25 | initDatabase();
26 | }
27 |
28 | public static void initDatabase()
29 | {
30 | Connection connection = null;
31 | Reader reader = null;
32 | try {
33 | connection = MyBatisSqlSessionFactory.getConnection();
34 | ScriptRunner scriptRunner = new ScriptRunner(connection);
35 | reader = Resources.getResourceAsReader("sql/drop_tables.sql");
36 | scriptRunner.runScript(reader);
37 | logger.info("drop_tables.sql executed successfully");
38 | reader = Resources.getResourceAsReader("sql/create_tables.sql");
39 | scriptRunner.runScript(reader );
40 | logger.info("create_tables.sql executed successfully");
41 | reader = Resources.getResourceAsReader("sql/sample_data.sql");
42 | scriptRunner.runScript(reader );
43 | logger.info("sample_data.sql executed successfully");
44 | connection.commit();
45 | reader.close();
46 | scriptRunner.closeConnection();
47 | } catch (Exception e) {
48 | throw new RuntimeException(e);
49 | }
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/chapter02/src/test/java/com/mybatis3/services/TestDataPopulator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.services;
5 |
6 | import java.io.Reader;
7 | import java.sql.Connection;
8 |
9 | import org.apache.ibatis.io.Resources;
10 | import org.apache.ibatis.jdbc.ScriptRunner;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.mybatis3.util.DataSourceFactory;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | public class TestDataPopulator
22 | {
23 | private static Logger logger = LoggerFactory.getLogger(TestDataPopulator.class);
24 |
25 | public static void main(String[] args) {
26 | initDatabase();
27 | }
28 |
29 | public static void initDatabase()
30 | {
31 | Connection connection = null;
32 | Reader reader = null;
33 | try {
34 | connection = DataSourceFactory.getDataSource().getConnection();
35 | ScriptRunner scriptRunner = new ScriptRunner(connection);
36 | reader = Resources.getResourceAsReader("sql/drop_tables.sql");
37 | scriptRunner.runScript(reader);
38 | logger.info("drop_tables.sql executed successfully");
39 | reader = Resources.getResourceAsReader("sql/create_tables.sql");
40 | scriptRunner.runScript(reader );
41 | logger.info("create_tables.sql executed successfully");
42 | reader = Resources.getResourceAsReader("sql/sample_data.sql");
43 | scriptRunner.runScript(reader );
44 | logger.info("sample_data.sql executed successfully");
45 | connection.commit();
46 | reader.close();
47 | scriptRunner.closeConnection();
48 | } catch (Exception e) {
49 | throw new RuntimeException(e);
50 | }
51 |
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/chapter03/src/main/resources/sql/sample_data.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | --Sample data for table ADDRESSES
4 |
5 | INSERT INTO ADDRESSES (ADDR_ID,STREET,CITY,STATE,ZIP,COUNTRY) VALUES
6 | (1,'4891 Pacific Hwy','San Diego','CA','92110','San Diego'),
7 | (2,'2400 N Jefferson St','Perry','FL','32347','Taylor'),
8 | (3,'710 N Cable Rd','Lima','OH','45825','Allen'),
9 | (4,'5108 W Gore Blvd','Lawton','OK','32365','Comanche');
10 |
11 | -- Sample data for table STUDENTS
12 |
13 | INSERT INTO STUDENTS (STUD_ID,NAME,EMAIL,PHONE,DOB,BIO,PIC,ADDR_ID) VALUES
14 | (1,'Timothy','timothy@gmail.com','123-123-1234','1988-04-25',NULL,NULL,3),
15 | (2,'Douglas','douglas@gmail.com','789-456-1234','1990-08-15',NULL,NULL,4);
16 |
17 | -- Sample data for table TUTORS
18 |
19 | INSERT INTO TUTORS (TUTOR_ID,NAME,EMAIL,PHONE,DOB,GENDER,BIO,PIC,ADDR_ID) VALUES
20 | (1,'John','john@gmail.com','111-222-3333','1980-05-20','MALE',NULL,NULL,1),
21 | (2,'Ken','ken@gmail.com','111-222-3333','1980-05-20','MALE',NULL,NULL,1),
22 | (3,'Paul','paul@gmail.com','123-321-4444','1981-03-15','FEMALE',NULL,NULL,2),
23 | (4,'Mike','mike@gmail.com','123-321-4444','1981-03-15','MALE',NULL,NULL,2);
24 |
25 | -- Sample data for table courses
26 |
27 | INSERT INTO COURSES (COURSE_ID,NAME,DESCRIPTION,START_DATE,END_DATE,TUTOR_ID) VALUES
28 | (1,'Quickstart Core Java','Core Java Programming','2013-03-01','2013-04-15',1),
29 | (2,'Quickstart JavaEE6','Enterprise App Development using JavaEE6','2013-04-01','2013-08-30',1),
30 | (3,'MyBatis3 Premier','MyBatis 3 framework','2013-06-01','2013-07-15',2);
31 |
32 | -- Sample data for table COURSE_ENROLLMENT
33 |
34 | INSERT INTO COURSE_ENROLLMENT (COURSE_ID,STUD_ID) VALUES
35 | (1,1),
36 | (1,2),
37 | (2,2);
38 |
39 |
--------------------------------------------------------------------------------
/chapter02/src/main/java/com/mybatis3/util/DataSourceFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.util;
5 |
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.util.Properties;
9 |
10 | import javax.naming.InitialContext;
11 | import javax.naming.NamingException;
12 | import javax.sql.DataSource;
13 |
14 | import org.apache.ibatis.datasource.pooled.PooledDataSource;
15 |
16 | /**
17 | * @author Siva
18 | *
19 | */
20 | public class DataSourceFactory
21 | {
22 | private static final Properties PROPERTIES = new Properties();
23 |
24 | static
25 | {
26 | try {
27 | InputStream is = DataSourceFactory.class.getResourceAsStream("/application.properties");
28 | PROPERTIES.load(is);
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | }
32 | }
33 |
34 | public static DataSource getDataSource()
35 | {
36 | String driver = PROPERTIES.getProperty("jdbc.driverClassName");
37 | String url = PROPERTIES.getProperty("jdbc.url");
38 | String username = PROPERTIES.getProperty("jdbc.username");
39 | String password = PROPERTIES.getProperty("jdbc.password");
40 | PooledDataSource dataSource = new PooledDataSource(driver, url, username, password);
41 | return dataSource;
42 | }
43 |
44 | public static DataSource getJNDIDataSource()
45 | {
46 | String dataSourceJNDIName = "java:comp/env/jdbc/MyBatisDemoDS";
47 | try
48 | {
49 | InitialContext ctx = new InitialContext();
50 | DataSource dataSource = (DataSource) ctx.lookup(dataSourceJNDIName);
51 | return dataSource;
52 | }
53 | catch (NamingException e)
54 | {
55 | throw new RuntimeException(e);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/chapter04/src/main/resources/sql/sample_data.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | --Sample data for table ADDRESSES
4 |
5 | INSERT INTO ADDRESSES (ADDR_ID,STREET,CITY,STATE,ZIP,COUNTRY) VALUES
6 | (1,'4891 Pacific Hwy','San Diego','CA','92110','San Diego'),
7 | (2,'2400 N Jefferson St','Perry','FL','32347','Taylor'),
8 | (3,'710 N Cable Rd','Lima','OH','45825','Allen'),
9 | (4,'5108 W Gore Blvd','Lawton','OK','32365','Comanche');
10 |
11 | -- Sample data for table STUDENTS
12 |
13 | INSERT INTO STUDENTS (STUD_ID,NAME,EMAIL,PHONE,DOB,GENDER,BIO,PIC,ADDR_ID) VALUES
14 | (1,'Timothy','timothy@gmail.com','123-123-1234','1988-04-25','MALE',NULL,NULL,3),
15 | (2,'Douglas','douglas@gmail.com','789-456-1234','1990-08-15','MALE',NULL,NULL,4);
16 |
17 | -- Sample data for table TUTORS
18 |
19 | INSERT INTO TUTORS (TUTOR_ID,NAME,EMAIL,PHONE,DOB,GENDER,BIO,PIC,ADDR_ID) VALUES
20 | (1,'John','john@gmail.com','111-222-3333','1980-05-20','MALE',NULL,NULL,1),
21 | (2,'Ken','ken@gmail.com','111-222-3333','1980-05-20','MALE',NULL,NULL,1),
22 | (3,'Paul','paul@gmail.com','123-321-4444','1981-03-15','FEMALE',NULL,NULL,2),
23 | (4,'Mike','mike@gmail.com','123-321-4444','1981-03-15','MALE',NULL,NULL,2);
24 |
25 | -- Sample data for table courses
26 |
27 | INSERT INTO COURSES (COURSE_ID,NAME,DESCRIPTION,START_DATE,END_DATE,TUTOR_ID) VALUES
28 | (1,'Quickstart Core Java','Core Java Programming','2013-03-01','2013-04-15',1),
29 | (2,'Quickstart JavaEE6','Enterprise App Development using JavaEE6','2013-04-01','2013-08-30',1),
30 | (3,'MyBatis3 Premier','MyBatis 3 framework','2013-06-01','2013-07-15',2);
31 |
32 | -- Sample data for table COURSE_ENROLLMENT
33 |
34 | INSERT INTO COURSE_ENROLLMENT (COURSE_ID,STUD_ID) VALUES
35 | (1,1),
36 | (1,2),
37 | (2,2);
38 |
39 |
--------------------------------------------------------------------------------
/chapter05/src/main/resources/sql/sample_data.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | --Sample data for table ADDRESSES
4 |
5 | INSERT INTO ADDRESSES (ADDR_ID,STREET,CITY,STATE,ZIP,COUNTRY) VALUES
6 | (1,'4891 Pacific Hwy','San Diego','CA','92110','San Diego'),
7 | (2,'2400 N Jefferson St','Perry','FL','32347','Taylor'),
8 | (3,'710 N Cable Rd','Lima','OH','45825','Allen'),
9 | (4,'5108 W Gore Blvd','Lawton','OK','32365','Comanche');
10 |
11 | -- Sample data for table STUDENTS
12 |
13 | INSERT INTO STUDENTS (STUD_ID,NAME,EMAIL,PHONE,DOB,GENDER,BIO,PIC,ADDR_ID) VALUES
14 | (1,'Timothy','timothy@gmail.com','123-123-1234','1988-04-25','MALE',NULL,NULL,3),
15 | (2,'Douglas','douglas@gmail.com','789-456-1234','1990-08-15','MALE',NULL,NULL,4);
16 |
17 | -- Sample data for table TUTORS
18 |
19 | INSERT INTO TUTORS (TUTOR_ID,NAME,EMAIL,PHONE,DOB,GENDER,BIO,PIC,ADDR_ID) VALUES
20 | (1,'John','john@gmail.com','111-222-3333','1980-05-20','MALE',NULL,NULL,1),
21 | (2,'Ken','ken@gmail.com','111-222-3333','1980-05-20','MALE',NULL,NULL,1),
22 | (3,'Paul','paul@gmail.com','123-321-4444','1981-03-15','FEMALE',NULL,NULL,2),
23 | (4,'Mike','mike@gmail.com','123-321-4444','1981-03-15','MALE',NULL,NULL,2);
24 |
25 | -- Sample data for table courses
26 |
27 | INSERT INTO COURSES (COURSE_ID,NAME,DESCRIPTION,START_DATE,END_DATE,TUTOR_ID) VALUES
28 | (1,'Quickstart Core Java','Core Java Programming','2013-03-01','2013-04-15',1),
29 | (2,'Quickstart JavaEE6','Enterprise App Development using JavaEE6','2013-04-01','2013-08-30',1),
30 | (3,'MyBatis3 Premier','MyBatis 3 framework','2013-06-01','2013-07-15',2);
31 |
32 | -- Sample data for table COURSE_ENROLLMENT
33 |
34 | INSERT INTO COURSE_ENROLLMENT (COURSE_ID,STUD_ID) VALUES
35 | (1,1),
36 | (1,2),
37 | (2,2);
38 |
39 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/domain/PhoneNumber.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.domain;
5 |
6 | import java.io.Serializable;
7 |
8 |
9 | /**
10 | * @author Siva
11 | *
12 | */
13 | public class PhoneNumber implements Serializable
14 | {
15 | private static final long serialVersionUID = 1L;
16 |
17 | private String countryCode;
18 | private String stateCode;
19 | private String number;
20 |
21 | public PhoneNumber() {
22 | }
23 |
24 | public PhoneNumber(String countryCode, String stateCode, String number) {
25 | super();
26 | this.countryCode = countryCode;
27 | this.stateCode = stateCode;
28 | this.number = number;
29 | }
30 |
31 | public PhoneNumber(String string) {
32 | if(string != null){
33 | String[] parts = string.split("-");
34 | if(parts.length>0) this.countryCode=parts[0];
35 | if(parts.length>1) this.stateCode=parts[1];
36 | if(parts.length>2) this.number=parts[2];
37 |
38 | }
39 | }
40 |
41 | /*@Override
42 | public String toString() {
43 | return this.getAsString();
44 | }*/
45 |
46 | public String getCountryCode() {
47 | return countryCode;
48 | }
49 |
50 | public void setCountryCode(String countryCode) {
51 | this.countryCode = countryCode;
52 | }
53 |
54 | public String getStateCode() {
55 | return stateCode;
56 | }
57 |
58 | public void setStateCode(String stateCode) {
59 | this.stateCode = stateCode;
60 | }
61 |
62 | public String getNumber() {
63 | return number;
64 | }
65 |
66 | public void setNumber(String number) {
67 | this.number = number;
68 | }
69 |
70 | public String getAsString() {
71 | return countryCode+"-"+stateCode+"-"+number;
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/domain/PhoneNumber.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.domain;
5 |
6 | import java.io.Serializable;
7 |
8 |
9 | /**
10 | * @author Siva
11 | *
12 | */
13 | public class PhoneNumber implements Serializable
14 | {
15 | private static final long serialVersionUID = 1L;
16 |
17 | private String countryCode;
18 | private String stateCode;
19 | private String number;
20 |
21 | public PhoneNumber() {
22 | }
23 |
24 | public PhoneNumber(String countryCode, String stateCode, String number) {
25 | super();
26 | this.countryCode = countryCode;
27 | this.stateCode = stateCode;
28 | this.number = number;
29 | }
30 |
31 | public PhoneNumber(String string) {
32 | if(string != null){
33 | String[] parts = string.split("-");
34 | if(parts.length>0) this.countryCode=parts[0];
35 | if(parts.length>1) this.stateCode=parts[1];
36 | if(parts.length>2) this.number=parts[2];
37 |
38 | }
39 | }
40 |
41 | /*@Override
42 | public String toString() {
43 | return this.getAsString();
44 | }*/
45 |
46 | public String getCountryCode() {
47 | return countryCode;
48 | }
49 |
50 | public void setCountryCode(String countryCode) {
51 | this.countryCode = countryCode;
52 | }
53 |
54 | public String getStateCode() {
55 | return stateCode;
56 | }
57 |
58 | public void setStateCode(String stateCode) {
59 | this.stateCode = stateCode;
60 | }
61 |
62 | public String getNumber() {
63 | return number;
64 | }
65 |
66 | public void setNumber(String number) {
67 | this.number = number;
68 | }
69 |
70 | public String getAsString() {
71 | return countryCode+"-"+stateCode+"-"+number;
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/domain/PhoneNumber.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.domain;
5 |
6 | import java.io.Serializable;
7 |
8 |
9 | /**
10 | * @author Siva
11 | *
12 | */
13 | public class PhoneNumber implements Serializable
14 | {
15 | private static final long serialVersionUID = 1L;
16 |
17 | private String countryCode;
18 | private String stateCode;
19 | private String number;
20 |
21 | public PhoneNumber() {
22 | }
23 |
24 | public PhoneNumber(String countryCode, String stateCode, String number) {
25 | super();
26 | this.countryCode = countryCode;
27 | this.stateCode = stateCode;
28 | this.number = number;
29 | }
30 |
31 | public PhoneNumber(String string) {
32 | if(string != null){
33 | String[] parts = string.split("-");
34 | if(parts.length>0) this.countryCode=parts[0];
35 | if(parts.length>1) this.stateCode=parts[1];
36 | if(parts.length>2) this.number=parts[2];
37 |
38 | }
39 | }
40 |
41 | /*@Override
42 | public String toString() {
43 | return this.getAsString();
44 | }*/
45 |
46 | public String getCountryCode() {
47 | return countryCode;
48 | }
49 |
50 | public void setCountryCode(String countryCode) {
51 | this.countryCode = countryCode;
52 | }
53 |
54 | public String getStateCode() {
55 | return stateCode;
56 | }
57 |
58 | public void setStateCode(String stateCode) {
59 | this.stateCode = stateCode;
60 | }
61 |
62 | public String getNumber() {
63 | return number;
64 | }
65 |
66 | public void setNumber(String number) {
67 | this.number = number;
68 | }
69 |
70 | public String getAsString() {
71 | return countryCode+"-"+stateCode+"-"+number;
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/chapter03/src/test/java/com/mybatis3/services/CourseServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import org.junit.AfterClass;
10 | import org.junit.Assert;
11 | import org.junit.BeforeClass;
12 | import org.junit.Test;
13 |
14 | import com.mybatis3.domain.Course;
15 |
16 | public class CourseServiceTest
17 | {
18 | private static CourseService courseService;
19 |
20 | @BeforeClass
21 | public static void setup()
22 | {
23 | TestDataPopulator.initDatabase();
24 | courseService = new CourseService();
25 | }
26 |
27 | @AfterClass
28 | public static void teardown()
29 | {
30 | courseService = null;
31 | }
32 |
33 | @Test
34 | public void searchCourses()
35 | {
36 | Map map = new HashMap();
37 | map.put("tutorId", 1);
38 | //map.put("courseName", "%java%");
39 | map.put("startDate", new Date());
40 | List courses = courseService.searchCourses(map);
41 | Assert.assertNotNull(courses);
42 | for (Course course : courses) {
43 | Assert.assertNotNull(course);
44 | //System.out.println(course);
45 | }
46 | }
47 |
48 | @Test
49 | public void searchCoursesByTutors()
50 | {
51 | Map map = new HashMap();
52 | List tutorIds = new ArrayList();
53 | tutorIds.add(1);
54 | tutorIds.add(2);
55 | map.put("tutorIds", tutorIds);
56 | map.put("courseName", "%java%");
57 | map.put("startDate", new Date());
58 | List courses = courseService.searchCoursesByTutors(map);
59 | Assert.assertNotNull(courses);
60 | for (Course course : courses) {
61 | Assert.assertNotNull(course);
62 | //System.out.println(course);
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/chapter03/src/main/resources/com/mybatis3/mappers/CourseMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
21 |
22 |
36 |
37 |
49 |
50 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/services/TutorService.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.mybatis3.services;
5 |
6 | import java.util.List;
7 |
8 | import org.apache.ibatis.session.SqlSession;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.stereotype.Service;
11 | import org.springframework.transaction.annotation.Transactional;
12 |
13 | import com.mybatis3.domain.Tutor;
14 | import com.mybatis3.mappers.TutorMapper;
15 |
16 |
17 | /**
18 | * @author Siva
19 | *
20 | */
21 | @Service
22 | @Transactional
23 | public class TutorService
24 | {
25 | @Autowired
26 | private SqlSession sqlSession;
27 |
28 | private TutorMapper getTutorMapper(){
29 | return sqlSession.getMapper(TutorMapper.class);
30 | }
31 | public List findAllTutors() {
32 | return getTutorMapper().findAllTutors();
33 | }
34 |
35 | public Tutor findTutorById(int tutorId) {
36 | return getTutorMapper().findTutorById(tutorId);
37 | }
38 |
39 | public Tutor findTutorByNameAndEmail(String name, String email) {
40 | return getTutorMapper().findTutorByNameAndEmail(name, email);
41 | }
42 |
43 | public Tutor createTutor(Tutor tutor) {
44 | getTutorMapper().insertTutor(tutor);
45 | return tutor;
46 | }
47 |
48 | public Tutor updateTutor(Tutor tutor) {
49 | getTutorMapper().updateTutor(tutor);
50 | return tutor;
51 | }
52 |
53 | public boolean deleteTutor(int tutorId) {
54 | boolean deleted = false;
55 | int nor = getTutorMapper().deleteTutor(tutorId);
56 | deleted = (nor == 1);
57 | return deleted;
58 | }
59 |
60 | public Tutor selectTutorById(int tutorId) {
61 | return getTutorMapper().selectTutorById(tutorId);
62 | }
63 |
64 | public Tutor selectTutorWithCoursesById(int tutorId) {
65 | return getTutorMapper().selectTutorWithCoursesById(tutorId);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/util/MyBatisUtil.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.util;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.sql.Connection;
6 | import java.sql.DriverManager;
7 | import java.util.Properties;
8 |
9 | import org.apache.ibatis.datasource.DataSourceFactory;
10 | import org.apache.ibatis.io.Resources;
11 | import org.apache.ibatis.session.SqlSessionFactory;
12 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
13 |
14 | /**
15 | * @author Siva
16 | *
17 | */
18 | public class MyBatisUtil
19 | {
20 | private static SqlSessionFactory sqlSessionFactory;
21 |
22 | private static final Properties PROPERTIES = new Properties();
23 |
24 | static
25 | {
26 | try {
27 | InputStream is = DataSourceFactory.class.getResourceAsStream("/application.properties");
28 | PROPERTIES.load(is);
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | }
32 | }
33 |
34 | public static Connection getConnection()
35 | {
36 | String driver = PROPERTIES.getProperty("jdbc.driverClassName");
37 | String url = PROPERTIES.getProperty("jdbc.url");
38 | String username = PROPERTIES.getProperty("jdbc.username");
39 | String password = PROPERTIES.getProperty("jdbc.password");
40 | Connection connection = null;
41 | try {
42 | Class.forName(driver);
43 | connection = DriverManager.getConnection(url, username, password);
44 | } catch (Exception e) {
45 | throw new RuntimeException(e);
46 | }
47 | return connection;
48 | }
49 |
50 | public static SqlSessionFactory getSqlSessionFactory()
51 | {
52 | if(sqlSessionFactory==null)
53 | {
54 | InputStream inputStream;
55 | try
56 | {
57 | inputStream = Resources.getResourceAsStream("mybatis-config.xml");
58 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
59 | }catch (IOException e)
60 | {
61 | throw new RuntimeException(e.getCause());
62 | }
63 | }
64 | return sqlSessionFactory;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/util/MyBatisUtil.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.util;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.sql.Connection;
6 | import java.sql.DriverManager;
7 | import java.util.Properties;
8 |
9 | import org.apache.ibatis.datasource.DataSourceFactory;
10 | import org.apache.ibatis.io.Resources;
11 | import org.apache.ibatis.session.SqlSessionFactory;
12 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
13 |
14 | /**
15 | * @author Siva
16 | *
17 | */
18 | public class MyBatisUtil
19 | {
20 | private static SqlSessionFactory sqlSessionFactory;
21 |
22 | private static final Properties PROPERTIES = new Properties();
23 |
24 | static
25 | {
26 | try {
27 | InputStream is = DataSourceFactory.class.getResourceAsStream("/application.properties");
28 | PROPERTIES.load(is);
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | }
32 | }
33 |
34 | public static Connection getConnection()
35 | {
36 | String driver = PROPERTIES.getProperty("jdbc.driverClassName");
37 | String url = PROPERTIES.getProperty("jdbc.url");
38 | String username = PROPERTIES.getProperty("jdbc.username");
39 | String password = PROPERTIES.getProperty("jdbc.password");
40 | Connection connection = null;
41 | try {
42 | Class.forName(driver);
43 | connection = DriverManager.getConnection(url, username, password);
44 | } catch (Exception e) {
45 | throw new RuntimeException(e);
46 | }
47 | return connection;
48 | }
49 |
50 | public static SqlSessionFactory getSqlSessionFactory()
51 | {
52 | if(sqlSessionFactory==null)
53 | {
54 | InputStream inputStream;
55 | try
56 | {
57 | inputStream = Resources.getResourceAsStream("mybatis-config.xml");
58 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
59 | }catch (IOException e)
60 | {
61 | throw new RuntimeException(e.getCause());
62 | }
63 | }
64 | return sqlSessionFactory;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/chapter03/src/main/resources/com/mybatis3/mappers/TutorMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
32 |
33 |
38 |
39 |
--------------------------------------------------------------------------------
/chapter02/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/domain/Address.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Siva
7 | *
8 | */
9 | public class Address implements Serializable
10 | {
11 | private static final long serialVersionUID = 1L;
12 |
13 | private Integer addrId;
14 | private String street;
15 | private String city;
16 | private String state;
17 | private String zip;
18 | private String country;
19 |
20 | @Override
21 | public String toString() {
22 | return "Address [addrId=" + addrId + ", street=" + street + ", city=" + city
23 | + ", state=" + state + ", zip=" + zip + ", country=" + country
24 | + "]";
25 | }
26 | public Address()
27 | {
28 | }
29 | public Address(Integer addrId)
30 | {
31 | this.addrId = addrId;
32 | }
33 | public Address(Integer addrId, String street, String city, String state,
34 | String zip, String country)
35 | {
36 | this.addrId = addrId;
37 | this.street = street;
38 | this.city = city;
39 | this.state = state;
40 | this.zip = zip;
41 | this.country = country;
42 | }
43 | public Integer getAddrId() {
44 | return addrId;
45 | }
46 | public void setAddrId(Integer addrId) {
47 | this.addrId = addrId;
48 | }
49 | public String getStreet()
50 | {
51 | return street;
52 | }
53 | public void setStreet(String street)
54 | {
55 | this.street = street;
56 | }
57 | public String getCity()
58 | {
59 | return city;
60 | }
61 | public void setCity(String city)
62 | {
63 | this.city = city;
64 | }
65 | public String getState()
66 | {
67 | return state;
68 | }
69 | public void setState(String state)
70 | {
71 | this.state = state;
72 | }
73 | public String getZip()
74 | {
75 | return zip;
76 | }
77 | public void setZip(String zip)
78 | {
79 | this.zip = zip;
80 | }
81 | public String getCountry()
82 | {
83 | return country;
84 | }
85 | public void setCountry(String country)
86 | {
87 | this.country = country;
88 | }
89 |
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/domain/Address.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Siva
7 | *
8 | */
9 | public class Address implements Serializable
10 | {
11 | private static final long serialVersionUID = 1L;
12 |
13 | private Integer addrId;
14 | private String street;
15 | private String city;
16 | private String state;
17 | private String zip;
18 | private String country;
19 |
20 | @Override
21 | public String toString() {
22 | return "Address [addrId=" + addrId + ", street=" + street + ", city=" + city
23 | + ", state=" + state + ", zip=" + zip + ", country=" + country
24 | + "]";
25 | }
26 | public Address()
27 | {
28 | }
29 | public Address(Integer addrId)
30 | {
31 | this.addrId = addrId;
32 | }
33 | public Address(Integer addrId, String street, String city, String state,
34 | String zip, String country)
35 | {
36 | this.addrId = addrId;
37 | this.street = street;
38 | this.city = city;
39 | this.state = state;
40 | this.zip = zip;
41 | this.country = country;
42 | }
43 | public Integer getAddrId() {
44 | return addrId;
45 | }
46 | public void setAddrId(Integer addrId) {
47 | this.addrId = addrId;
48 | }
49 | public String getStreet()
50 | {
51 | return street;
52 | }
53 | public void setStreet(String street)
54 | {
55 | this.street = street;
56 | }
57 | public String getCity()
58 | {
59 | return city;
60 | }
61 | public void setCity(String city)
62 | {
63 | this.city = city;
64 | }
65 | public String getState()
66 | {
67 | return state;
68 | }
69 | public void setState(String state)
70 | {
71 | this.state = state;
72 | }
73 | public String getZip()
74 | {
75 | return zip;
76 | }
77 | public void setZip(String zip)
78 | {
79 | this.zip = zip;
80 | }
81 | public String getCountry()
82 | {
83 | return country;
84 | }
85 | public void setCountry(String country)
86 | {
87 | this.country = country;
88 | }
89 |
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/domain/Address.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Siva
7 | *
8 | */
9 | public class Address implements Serializable
10 | {
11 | private static final long serialVersionUID = 1L;
12 |
13 | private Integer addrId;
14 | private String street;
15 | private String city;
16 | private String state;
17 | private String zip;
18 | private String country;
19 |
20 | @Override
21 | public String toString() {
22 | return "Address [addrId=" + addrId + ", street=" + street + ", city=" + city
23 | + ", state=" + state + ", zip=" + zip + ", country=" + country
24 | + "]";
25 | }
26 | public Address()
27 | {
28 | }
29 | public Address(Integer addrId)
30 | {
31 | this.addrId = addrId;
32 | }
33 | public Address(Integer addrId, String street, String city, String state,
34 | String zip, String country)
35 | {
36 | this.addrId = addrId;
37 | this.street = street;
38 | this.city = city;
39 | this.state = state;
40 | this.zip = zip;
41 | this.country = country;
42 | }
43 | public Integer getAddrId() {
44 | return addrId;
45 | }
46 | public void setAddrId(Integer addrId) {
47 | this.addrId = addrId;
48 | }
49 | public String getStreet()
50 | {
51 | return street;
52 | }
53 | public void setStreet(String street)
54 | {
55 | this.street = street;
56 | }
57 | public String getCity()
58 | {
59 | return city;
60 | }
61 | public void setCity(String city)
62 | {
63 | this.city = city;
64 | }
65 | public String getState()
66 | {
67 | return state;
68 | }
69 | public void setState(String state)
70 | {
71 | this.state = state;
72 | }
73 | public String getZip()
74 | {
75 | return zip;
76 | }
77 | public void setZip(String zip)
78 | {
79 | this.zip = zip;
80 | }
81 | public String getCountry()
82 | {
83 | return country;
84 | }
85 | public void setCountry(String country)
86 | {
87 | this.country = country;
88 | }
89 |
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/chapter01/src/main/java/com/mybatis3/services/StudentService.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import java.util.List;
4 |
5 | import org.apache.ibatis.session.SqlSession;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 |
9 | import com.mybatis3.domain.Student;
10 | import com.mybatis3.mappers.StudentMapper;
11 | import com.mybatis3.util.MyBatisSqlSessionFactory;
12 |
13 |
14 | /**
15 | * @author Siva
16 | *
17 | */
18 | public class StudentService
19 | {
20 | private Logger logger = LoggerFactory.getLogger(getClass());
21 |
22 | public List findAllStudents()
23 | {
24 | SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
25 | try {
26 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
27 | return studentMapper.findAllStudents();
28 | } finally {
29 | sqlSession.close();
30 | }
31 | }
32 |
33 | public Student findStudentById(Integer studId)
34 | {
35 | logger.debug("Select Student By ID :{}", studId);
36 | SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
37 | try {
38 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
39 | return studentMapper.findStudentById(studId);
40 | //return sqlSession.selectOne("com.mybatis3.StudentMapper.findStudentById", studId);
41 | } finally {
42 | sqlSession.close();
43 | }
44 | }
45 |
46 | public void createStudent(Student student)
47 | {
48 | SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
49 | try {
50 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
51 | studentMapper.insertStudent(student);
52 | sqlSession.commit();
53 | } finally {
54 | sqlSession.close();
55 | }
56 | }
57 |
58 | public void updateStudent(Student student)
59 | {
60 | SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
61 | try {
62 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
63 | studentMapper.updateStudent(student);
64 | sqlSession.commit();
65 | } finally {
66 | sqlSession.close();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/domain/Course.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.ArrayList;
5 | import java.util.Date;
6 | import java.util.List;
7 |
8 | /**
9 | * @author Siva
10 | *
11 | */
12 | public class Course implements Serializable
13 | {
14 | private static final long serialVersionUID = 1L;
15 |
16 | private Integer courseId;
17 | private String name;
18 | private String description;
19 | private Date startDate;
20 | private Date endDate;
21 | private Tutor tutor;
22 | private List students;
23 |
24 | @Override
25 | public String toString() {
26 | return "Course [courseId=" + courseId + ", name=" + name + ", description="
27 | + description + ", startDate=" + startDate + ", endDate="
28 | + endDate + ", tutor=" + tutor + ", students=" + students + "]";
29 | }
30 | public Integer getCourseId()
31 | {
32 | return courseId;
33 | }
34 | public void setCourseId(Integer id)
35 | {
36 | this.courseId = id;
37 | }
38 | public String getName()
39 | {
40 | return name;
41 | }
42 | public void setName(String name)
43 | {
44 | this.name = name;
45 | }
46 | public String getDescription()
47 | {
48 | return description;
49 | }
50 | public void setDescription(String description)
51 | {
52 | this.description = description;
53 | }
54 | public Date getStartDate()
55 | {
56 | return startDate;
57 | }
58 | public void setStartDate(Date startDate)
59 | {
60 | this.startDate = startDate;
61 | }
62 | public Date getEndDate()
63 | {
64 | return endDate;
65 | }
66 | public void setEndDate(Date endDate)
67 | {
68 | this.endDate = endDate;
69 | }
70 | public List getStudents()
71 | {
72 | if(students == null){
73 | students = new ArrayList(0);
74 | }
75 | return students;
76 | }
77 | public void setStudents(List students)
78 | {
79 | this.students = students;
80 | }
81 | public Tutor getTutor() {
82 | return tutor;
83 | }
84 | public void setTutor(Tutor tutor) {
85 | this.tutor = tutor;
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/chapter04/src/main/java/com/mybatis3/domain/Course.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.ArrayList;
5 | import java.util.Date;
6 | import java.util.List;
7 |
8 | /**
9 | * @author Siva
10 | *
11 | */
12 | public class Course implements Serializable
13 | {
14 | private static final long serialVersionUID = 1L;
15 |
16 | private Integer courseId;
17 | private String name;
18 | private String description;
19 | private Date startDate;
20 | private Date endDate;
21 | private Tutor tutor;
22 | private List students;
23 |
24 | @Override
25 | public String toString() {
26 | return "Course [courseId=" + courseId + ", name=" + name + ", description="
27 | + description + ", startDate=" + startDate + ", endDate="
28 | + endDate + ", tutor=" + tutor + ", students=" + students + "]";
29 | }
30 | public Integer getCourseId()
31 | {
32 | return courseId;
33 | }
34 | public void setCourseId(Integer id)
35 | {
36 | this.courseId = id;
37 | }
38 | public String getName()
39 | {
40 | return name;
41 | }
42 | public void setName(String name)
43 | {
44 | this.name = name;
45 | }
46 | public String getDescription()
47 | {
48 | return description;
49 | }
50 | public void setDescription(String description)
51 | {
52 | this.description = description;
53 | }
54 | public Date getStartDate()
55 | {
56 | return startDate;
57 | }
58 | public void setStartDate(Date startDate)
59 | {
60 | this.startDate = startDate;
61 | }
62 | public Date getEndDate()
63 | {
64 | return endDate;
65 | }
66 | public void setEndDate(Date endDate)
67 | {
68 | this.endDate = endDate;
69 | }
70 | public List getStudents()
71 | {
72 | if(students == null){
73 | students = new ArrayList(0);
74 | }
75 | return students;
76 | }
77 | public void setStudents(List students)
78 | {
79 | this.students = students;
80 | }
81 | public Tutor getTutor() {
82 | return tutor;
83 | }
84 | public void setTutor(Tutor tutor) {
85 | this.tutor = tutor;
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/domain/Course.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.ArrayList;
5 | import java.util.Date;
6 | import java.util.List;
7 |
8 | /**
9 | * @author Siva
10 | *
11 | */
12 | public class Course implements Serializable
13 | {
14 | private static final long serialVersionUID = 1L;
15 |
16 | private Integer courseId;
17 | private String name;
18 | private String description;
19 | private Date startDate;
20 | private Date endDate;
21 | private Tutor tutor;
22 | private List students;
23 |
24 | @Override
25 | public String toString() {
26 | return "Course [courseId=" + courseId + ", name=" + name + ", description="
27 | + description + ", startDate=" + startDate + ", endDate="
28 | + endDate + ", tutor=" + tutor + ", students=" + students + "]";
29 | }
30 | public Integer getCourseId()
31 | {
32 | return courseId;
33 | }
34 | public void setCourseId(Integer id)
35 | {
36 | this.courseId = id;
37 | }
38 | public String getName()
39 | {
40 | return name;
41 | }
42 | public void setName(String name)
43 | {
44 | this.name = name;
45 | }
46 | public String getDescription()
47 | {
48 | return description;
49 | }
50 | public void setDescription(String description)
51 | {
52 | this.description = description;
53 | }
54 | public Date getStartDate()
55 | {
56 | return startDate;
57 | }
58 | public void setStartDate(Date startDate)
59 | {
60 | this.startDate = startDate;
61 | }
62 | public Date getEndDate()
63 | {
64 | return endDate;
65 | }
66 | public void setEndDate(Date endDate)
67 | {
68 | this.endDate = endDate;
69 | }
70 | public List getStudents()
71 | {
72 | if(students == null){
73 | students = new ArrayList(0);
74 | }
75 | return students;
76 | }
77 | public void setStudents(List students)
78 | {
79 | this.students = students;
80 | }
81 | public Tutor getTutor() {
82 | return tutor;
83 | }
84 | public void setTutor(Tutor tutor) {
85 | this.tutor = tutor;
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/chapter03/src/main/java/com/mybatis3/util/MyBatisUtil.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.util;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.sql.Connection;
6 | import java.sql.DriverManager;
7 | import java.util.Properties;
8 |
9 | import org.apache.ibatis.datasource.DataSourceFactory;
10 | import org.apache.ibatis.io.Resources;
11 | import org.apache.ibatis.session.SqlSessionFactory;
12 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
13 |
14 | /**
15 | * @author Siva
16 | *
17 | */
18 | public class MyBatisUtil
19 | {
20 | private static final String DEFAULT_MYBATIS_CONFIG_FILE="mybatis-config.xml";
21 | private static SqlSessionFactory sqlSessionFactory;
22 |
23 | private static final Properties PROPERTIES = new Properties();
24 |
25 | static
26 | {
27 | try {
28 | InputStream is = DataSourceFactory.class.getResourceAsStream("/application.properties");
29 | PROPERTIES.load(is);
30 | } catch (IOException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 | public static SqlSessionFactory getSqlSessionFactory()
35 | {
36 | if(sqlSessionFactory==null){
37 | //org.apache.ibatis.logging.LogFactory.useLog4JLogging();
38 | try
39 | {
40 | InputStream inputStream = Resources.getResourceAsStream(DEFAULT_MYBATIS_CONFIG_FILE);
41 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
42 | } catch (IOException e)
43 | {
44 | throw new RuntimeException(e.getCause());
45 | }
46 | }
47 | return sqlSessionFactory;
48 | }
49 |
50 |
51 | public static Connection getConnection()
52 | {
53 | String driver = PROPERTIES.getProperty("jdbc.driverClassName");
54 | String url = PROPERTIES.getProperty("jdbc.url");
55 | String username = PROPERTIES.getProperty("jdbc.username");
56 | String password = PROPERTIES.getProperty("jdbc.password");
57 | Connection connection = null;
58 | try {
59 | Class.forName(driver);
60 | connection = DriverManager.getConnection(url, username, password);
61 | } catch (Exception e) {
62 | throw new RuntimeException(e);
63 | }
64 | return connection;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/chapter02/src/main/java/com/mybatis3/services/StudentService.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import java.util.List;
4 |
5 | import org.apache.ibatis.session.SqlSession;
6 | import org.apache.ibatis.session.SqlSessionFactory;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 |
10 | import com.mybatis3.domain.Student;
11 | import com.mybatis3.mappers.StudentMapper;
12 |
13 |
14 | public class StudentService
15 | {
16 | private Logger logger = LoggerFactory.getLogger(getClass());
17 |
18 | private SqlSessionFactory factory;
19 | public StudentService(SqlSessionFactory factory) {
20 | this.factory = factory;
21 | }
22 |
23 | protected SqlSession openSqlSession()
24 | {
25 | return factory.openSession();
26 | }
27 | public List findAllStudents()
28 | {
29 | SqlSession sqlSession = openSqlSession();
30 | try {
31 | //sqlSession.selectList("com.mybatis3.mappers.StudentMapper.findAllStudents");
32 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
33 | return studentMapper.findAllStudents();
34 | } finally {
35 | sqlSession.close();
36 | }
37 | }
38 |
39 | public Student findStudentById(Integer studId)
40 | {
41 | logger.debug("Select Student By ID :{}", studId);
42 | SqlSession sqlSession = openSqlSession();
43 | try {
44 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
45 | return studentMapper.findStudentById(studId);
46 | } finally {
47 | sqlSession.close();
48 | }
49 | }
50 |
51 | public Student createStudent(Student student)
52 | {
53 | SqlSession sqlSession = openSqlSession();
54 | try {
55 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
56 | studentMapper.insertStudent(student);
57 | sqlSession.commit();
58 | return student;
59 | } finally {
60 | sqlSession.close();
61 | }
62 | }
63 |
64 | public void updateStudent(Student student)
65 | {
66 | SqlSession sqlSession = openSqlSession();
67 | try {
68 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
69 | studentMapper.updateStudent(student);
70 | sqlSession.commit();
71 | } finally {
72 | sqlSession.close();
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/chapter05/src/main/java/com/mybatis3/services/StudentService.java:
--------------------------------------------------------------------------------
1 | package com.mybatis3.services;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.stereotype.Service;
10 | import org.springframework.transaction.annotation.Transactional;
11 |
12 | import com.mybatis3.domain.Address;
13 | import com.mybatis3.domain.Student;
14 | import com.mybatis3.mappers.AddressMapper;
15 | import com.mybatis3.mappers.StudentMapper;
16 |
17 |
18 | @Service
19 | @Transactional
20 | public class StudentService
21 | {
22 | private Logger logger = LoggerFactory.getLogger(getClass());
23 |
24 | @Autowired
25 | private StudentMapper studentMapper;
26 |
27 | @Autowired
28 | private AddressMapper addressMapper;
29 |
30 | public List findAllStudents() {
31 | return studentMapper.findAllStudents();
32 | }
33 |
34 | public Student findStudentById(Integer id) {
35 | logger.debug("findStudentById :"+id);
36 | return studentMapper.findStudentById(id);
37 | }
38 |
39 | public Student findStudentWithAddressById(int id) {
40 | return studentMapper.selectStudentWithAddress(id);
41 | }
42 |
43 | public Student createStudent(Student student) {
44 | Address address = student.getAddress();
45 | if(address != null){
46 | addressMapper.insertAddress(address);
47 | }
48 | if(student.getName()==null || student.getName().trim().length()==0){
49 | throw new RuntimeException("Student Name should not be null");
50 | }
51 | studentMapper.insertStudent(student);
52 | return student;
53 | }
54 |
55 | public void createStudentWithMap(Map studentDataMap) {
56 | studentMapper.insertStudentWithMap(studentDataMap);
57 | }
58 |
59 | public Student updateStudent(Student student) {
60 | studentMapper.updateStudent(student);
61 | return student;
62 | }
63 |
64 | public boolean deleteStudent(int id) {
65 | int count = studentMapper.deleteStudent(id);
66 | return count > 0;
67 | }
68 |
69 | public Map findStudentMapById(int id) {
70 | return studentMapper.findStudentMapById(id);
71 | }
72 |
73 | public List