Skip to content

Commit 7ecb6ba

Browse files
Refactored each one as a separate module.
1 parent ac86eb7 commit 7ecb6ba

File tree

112 files changed

+1333
-357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1333
-357
lines changed

chapter01/README.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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.

chapter01/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.mybatis3</groupId>
8+
<artifactId>com.mybatis3</artifactId>
9+
<version>0.0.1</version>
10+
<packaging>jar</packaging>
11+
12+
<name>com.mybatis3</name>
13+
<url>http://www.mybatis.org</url>
14+
<description>MyBatis Book Chapter 01</description>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<java.version>1.6</java.version>
19+
<junit.version>4.8.2</junit.version>
20+
<slf4j.version>1.6.2</slf4j.version>
21+
<logback.version>1.0.1</logback.version>
22+
<log4j.version>1.2.16</log4j.version>
23+
<mybatis.version>3.1.1</mybatis.version>
24+
<mysql.version>5.1.18</mysql.version>
25+
<maven.compiler.plugin>2.3.2</maven.compiler.plugin>
26+
</properties>
27+
28+
<build>
29+
<finalName>${project.artifactId}</finalName>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>${maven.compiler.plugin}</version>
35+
<configuration>
36+
<source>${java.version}</source>
37+
<target>${java.version}</target>
38+
<encoding>${project.build.sourceEncoding}</encoding>
39+
</configuration>
40+
</plugin>
41+
42+
</plugins>
43+
</build>
44+
45+
<dependencies>
46+
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>${junit.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.mybatis</groupId>
56+
<artifactId>mybatis</artifactId>
57+
<version>${mybatis.version}</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>org.slf4j</groupId>
62+
<artifactId>slf4j-api</artifactId>
63+
<version>${slf4j.version}</version>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>org.slf4j</groupId>
68+
<artifactId>slf4j-log4j12</artifactId>
69+
<version>${slf4j.version}</version>
70+
<scope>runtime</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>log4j</groupId>
75+
<artifactId>log4j</artifactId>
76+
<version>${log4j.version}</version>
77+
<scope>runtime</scope>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>mysql</groupId>
82+
<artifactId>mysql-connector-java</artifactId>
83+
<version>${mysql.version}</version>
84+
<scope>runtime</scope>
85+
</dependency>
86+
87+
</dependencies>
88+
89+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

mybatisdemo-parent/chapter01/src/main/java/chapter01/StudentMapper.java renamed to chapter01/src/main/java/com/mybatis3/mappers/StudentMapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package chapter01;
1+
package com.mybatis3.mappers;
22

33
import java.util.List;
44

5-
import chapter01.domain.Student;
5+
import com.mybatis3.domain.Student;
6+
67

78
/**
89
* @author Siva

mybatisdemo-parent/chapter01/src/main/java/chapter01/JdbcStudentService.java renamed to chapter01/src/main/java/com/mybatis3/services/JdbcStudentService.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
1-
package chapter01;
1+
package com.mybatis3.services;
22

33
import java.sql.Connection;
44
import java.sql.DriverManager;
55
import java.sql.PreparedStatement;
66
import java.sql.ResultSet;
77
import java.sql.SQLException;
8+
import java.util.Date;
9+
10+
import com.mybatis3.domain.Student;
811

9-
import chapter01.domain.Student;
1012

1113
/**
1214
* @author Siva
1315
*
1416
*/
17+
1518
public class JdbcStudentService
1619
{
20+
21+
private static final String DRIVER = "com.mysql.jdbc.Driver";
22+
private static final String URL = "jdbc:mysql://localhost:3306/elearning";
23+
private static final String USERNAME = "root";
24+
private static final String PASSWORD = "admin";
25+
1726
public static void main(String[] args)
1827
{
28+
1929
JdbcStudentService service = new JdbcStudentService();
20-
Student student = service.findStudentById(1);
21-
System.out.println(student);
22-
//student.setId(2);
23-
//service.createStudent(student);
24-
student.setId(2);
25-
student.setEmail("student2@gmail.com");
26-
service.updateStudent(student);
30+
31+
Student existingStudent = service.findStudentById(1);
32+
System.out.println(existingStudent);
33+
34+
35+
long ts = System.currentTimeMillis();//For creating unique student names
36+
Student newStudent = new Student(0,"student_"+ts,"student_"+ts+"@gmail.com",new Date());
37+
service.createStudent(newStudent);
38+
System.out.println(newStudent);
39+
40+
int updateStudId = 3;
41+
Student updateStudent = service.findStudentById(updateStudId);
42+
ts = System.currentTimeMillis();//For creating unique student email
43+
updateStudent.setEmail("student_"+ts+"@gmail.com");
44+
service.updateStudent(updateStudent);
45+
2746
}
2847

2948
public Student findStudentById(int studId)
@@ -40,7 +59,7 @@ public Student findStudentById(int studId)
4059
if(rs.next())
4160
{
4261
student = new Student();
43-
student.setId(rs.getInt("stud_id"));
62+
student.setStudId(rs.getInt("stud_id"));
4463
student.setName(rs.getString("name"));
4564
student.setEmail(rs.getString("email"));
4665
student.setDob(rs.getDate("dob"));
@@ -69,7 +88,7 @@ public void createStudent(Student student)
6988
conn = getDatabaseConnection();
7089
String sql = "INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB) VALUES(?,?,?,?)";
7190
PreparedStatement pstmt = conn.prepareStatement(sql);
72-
pstmt.setInt(1, student.getId());
91+
pstmt.setInt(1, student.getStudId());
7392
pstmt.setString(2, student.getName());
7493
pstmt.setString(3, student.getEmail());
7594
pstmt.setDate(4, new java.sql.Date(student.getDob().getTime()));
@@ -101,7 +120,7 @@ public void updateStudent(Student student)
101120
pstmt.setString(1, student.getName());
102121
pstmt.setString(2, student.getEmail());
103122
pstmt.setDate(3, new java.sql.Date(student.getDob().getTime()));
104-
pstmt.setInt(4, student.getId());
123+
pstmt.setInt(4, student.getStudId());
105124
pstmt.executeUpdate();
106125

107126
} catch (SQLException e)
@@ -122,9 +141,10 @@ protected Connection getDatabaseConnection() throws SQLException
122141
{
123142
try
124143
{
125-
Class.forName("com.mysql.jdbc.Driver");
126-
return DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatisdemo",
127-
"root", "admin");
144+
Class.forName(JdbcStudentService.DRIVER);
145+
return DriverManager.getConnection(JdbcStudentService.URL,
146+
JdbcStudentService.USERNAME,
147+
JdbcStudentService.PASSWORD);
128148
} catch (SQLException e)
129149
{
130150
throw e;

mybatisdemo-parent/chapter01/src/main/java/chapter01/StudentService.java renamed to chapter01/src/main/java/com/mybatis3/services/StudentService.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
package chapter01;
1+
package com.mybatis3.services;
22

33
import java.util.List;
44

55
import org.apache.ibatis.session.SqlSession;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9-
import chapter01.domain.Student;
9+
import com.mybatis3.domain.Student;
10+
import com.mybatis3.mappers.StudentMapper;
11+
import com.mybatis3.util.MyBatisSqlSessionFactory;
1012

13+
14+
/**
15+
* @author Siva
16+
*
17+
*/
1118
public class StudentService
1219
{
1320
private Logger logger = LoggerFactory.getLogger(getClass());
1421

1522
public List<Student> findAllStudents()
1623
{
17-
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
24+
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
1825
try {
1926
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
2027
return studentMapper.findAllStudents();
@@ -26,19 +33,19 @@ public List<Student> findAllStudents()
2633
public Student findStudentById(Integer studId)
2734
{
2835
logger.debug("Select Student By ID :{}", studId);
29-
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
36+
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
3037
try {
3138
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
3239
return studentMapper.findStudentById(studId);
33-
//return sqlSession.selectOne("chapter01.StudentMapper.findStudentById", studId);
40+
//return sqlSession.selectOne("com.mybatis3.StudentMapper.findStudentById", studId);
3441
} finally {
3542
sqlSession.close();
3643
}
3744
}
3845

3946
public void createStudent(Student student)
4047
{
41-
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
48+
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
4249
try {
4350
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
4451
studentMapper.insertStudent(student);
@@ -50,7 +57,7 @@ public void createStudent(Student student)
5057

5158
public void updateStudent(Student student)
5259
{
53-
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
60+
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
5461
try {
5562
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
5663
studentMapper.updateStudent(student);

mybatisdemo-parent/chapter01/src/main/java/chapter01/MyBatisUtil.java renamed to chapter01/src/main/java/com/mybatis3/util/MyBatisSqlSessionFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
package chapter01;
1+
package com.mybatis3.util;
22

33
import java.io.IOException;
44
import java.io.InputStream;
55

66
import org.apache.ibatis.io.Resources;
7+
import org.apache.ibatis.session.SqlSession;
78
import org.apache.ibatis.session.SqlSessionFactory;
89
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
910

1011
/**
1112
* @author Siva
1213
*
1314
*/
14-
public class MyBatisUtil
15+
public class MyBatisSqlSessionFactory
1516
{
1617
private static SqlSessionFactory sqlSessionFactory;
1718

@@ -31,4 +32,9 @@ public static SqlSessionFactory getSqlSessionFactory()
3132
}
3233
return sqlSessionFactory;
3334
}
35+
36+
public static SqlSession getSqlSession()
37+
{
38+
return getSqlSessionFactory().openSession();
39+
}
3440
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
################### DataSource Configuration ##########################
44

55
jdbc.driverClassName=com.mysql.jdbc.Driver
6-
jdbc.url=jdbc:mysql://localhost:3306/mybatisdemo
6+
jdbc.url=jdbc:mysql://localhost:3306/elearning
77
jdbc.username=root
88
jdbc.password=admin

0 commit comments

Comments
 (0)