Skip to content

Commit 2203fa8

Browse files
Added Test SQL Scripts for all modules.
1 parent dafb7a6 commit 2203fa8

File tree

15 files changed

+180
-86
lines changed

15 files changed

+180
-86
lines changed

chapter01/src/main/java/com/mybatis3/util/MyBatisSqlSessionFactory.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.util.Properties;
58

9+
import org.apache.ibatis.datasource.DataSourceFactory;
610
import org.apache.ibatis.io.Resources;
711
import org.apache.ibatis.session.SqlSession;
812
import org.apache.ibatis.session.SqlSessionFactory;
@@ -16,6 +20,18 @@ public class MyBatisSqlSessionFactory
1620
{
1721
private static SqlSessionFactory sqlSessionFactory;
1822

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+
1935
public static SqlSessionFactory getSqlSessionFactory()
2036
{
2137
if(sqlSessionFactory==null)
@@ -37,4 +53,20 @@ public static SqlSession getSqlSession()
3753
{
3854
return getSqlSessionFactory().openSession();
3955
}
56+
57+
public static Connection getConnection()
58+
{
59+
String driver = PROPERTIES.getProperty("jdbc.driverClassName");
60+
String url = PROPERTIES.getProperty("jdbc.url");
61+
String username = PROPERTIES.getProperty("jdbc.username");
62+
String password = PROPERTIES.getProperty("jdbc.password");
63+
Connection connection = null;
64+
try {
65+
Class.forName(driver);
66+
connection = DriverManager.getConnection(url, username, password);
67+
} catch (Exception e) {
68+
throw new RuntimeException(e);
69+
}
70+
return connection;
71+
}
4072
}

chapter01/src/main/resources/sql/create_tables.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
3-
CREATE DATABASE IF NOT EXISTS ELEARNING;
4-
USE ELEARNING;
5-
62
CREATE TABLE ADDRESSES
73
(
84
ADDR_ID INT(11) NOT NULL AUTO_INCREMENT,

chapter01/src/main/resources/sql/drop_tables.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
DROP TABLE IF EXISTS USER_PICS;
23
DROP TABLE IF EXISTS COURSE_ENROLLMENT;
34
DROP TABLE IF EXISTS COURSES;
45
DROP TABLE IF EXISTS TUTORS;

chapter01/src/test/java/com/mybatis3/services/StudentServiceTest.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import java.util.List;
55

66
import org.junit.AfterClass;
7-
import org.junit.Assert;
7+
import static org.junit.Assert.*;
88
import org.junit.BeforeClass;
9-
import org.junit.Ignore;
109
import org.junit.Test;
1110

1211
import com.mybatis3.domain.Student;
@@ -25,6 +24,7 @@ public class StudentServiceTest
2524
public static void setup()
2625
{
2726
studentService = new StudentService();
27+
TestDataPopulator.initDatabase();
2828
}
2929
@AfterClass
3030
public static void teardown()
@@ -36,10 +36,11 @@ public static void teardown()
3636
public void testFindAllStudents()
3737
{
3838
List<Student> students = studentService.findAllStudents();
39-
Assert.assertNotNull(students);
39+
assertNotNull(students);
4040
for (Student student : students)
4141
{
42-
System.out.println(student);
42+
assertNotNull(student);
43+
//System.out.println(student);
4344
}
4445

4546
}
@@ -48,12 +49,10 @@ public void testFindAllStudents()
4849
public void testFindStudentById()
4950
{
5051
Student student = studentService.findStudentById(1);
51-
Assert.assertNotNull(student);
52-
System.out.println(student);
52+
assertNotNull(student);
5353
}
5454

5555
@Test
56-
@Ignore
5756
public void testCreateUStudent()
5857
{
5958
Student student = new Student();
@@ -64,14 +63,15 @@ public void testCreateUStudent()
6463
student.setDob(new Date());
6564
studentService.createStudent(student);
6665
Student newStudent = studentService.findStudentById(id);
67-
Assert.assertNotNull(newStudent);
66+
assertNotNull(newStudent);
67+
assertEquals("student_"+id, newStudent.getName());
68+
assertEquals("student_"+id+"gmail.com", newStudent.getEmail());
6869
}
6970

70-
@Test
71-
//@Ignore
71+
@Test
7272
public void testUpdateStudent()
7373
{
74-
int id = 3;
74+
int id = 2;
7575
Student student =studentService.findStudentById(id);
7676
student.setStudId(id);
7777
student.setName("student_"+id);
@@ -80,6 +80,9 @@ public void testUpdateStudent()
8080
student.setDob(now);
8181
studentService.updateStudent(student);
8282
Student updatedStudent = studentService.findStudentById(id);
83-
Assert.assertNotNull(updatedStudent);
83+
assertNotNull(updatedStudent);
84+
assertEquals("student_"+id, updatedStudent.getName());
85+
assertEquals("student_"+id+"gmail.com", updatedStudent.getEmail());
86+
8487
}
8588
}

chapter02/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
66
<modelVersion>4.0.0</modelVersion>
77

8-
<groupId>com.mybatisdemo</groupId>
9-
<version>0.0.1</version>
8+
<groupId>com.mybatis3</groupId>
109
<artifactId>chapter02</artifactId>
10+
<version>0.0.1</version>
1111
<name>chapter02</name>
1212
<url>http://www.mybatis.org</url>
1313
<description>MyBatis Book Chapter 02</description>

chapter02/src/main/java/com/mybatis3/domain/Student.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,54 @@
1212
@Alias("Student")
1313
public class Student
1414
{
15-
private Integer id;
15+
private Integer studId;
1616
private String name;
1717
private String email;
1818
private Date dob;
1919

20-
@Override
21-
public String toString()
22-
{
23-
return "Student [id=" + id + ", name=" + name + ", email=" + email
24-
+ ", dob=" + dob + "]";
20+
public Student() {
21+
2522
}
26-
public Student()
27-
{
23+
24+
public Student(Integer studId) {
25+
this.studId = studId;
2826
}
29-
public Student(Integer id)
30-
{
31-
this.id = id;
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;
3233
}
33-
public Integer getId()
34-
{
35-
return id;
34+
35+
@Override
36+
public String toString() {
37+
return "Student [studId=" + studId + ", name=" + name + ", email="
38+
+ email + ", dob=" + dob + "]";
3639
}
37-
public void setId(Integer id)
38-
{
39-
this.id = id;
40+
41+
public Integer getStudId() {
42+
return studId;
4043
}
41-
public String getName()
42-
{
44+
public void setStudId(Integer studId) {
45+
this.studId = studId;
46+
}
47+
public String getName() {
4348
return name;
4449
}
45-
public void setName(String name)
46-
{
50+
public void setName(String name) {
4751
this.name = name;
4852
}
49-
public String getEmail()
50-
{
53+
public String getEmail() {
5154
return email;
5255
}
53-
public void setEmail(String email)
54-
{
56+
public void setEmail(String email) {
5557
this.email = email;
5658
}
57-
public Date getDob()
58-
{
59+
public Date getDob() {
5960
return dob;
6061
}
61-
public void setDob(Date dob)
62-
{
62+
public void setDob(Date dob) {
6363
this.dob = dob;
6464
}
65-
6665
}

chapter02/src/main/java/com/mybatis3/services/StudentService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ public Student findStudentById(Integer studId)
4747
}
4848
}
4949

50-
public void createStudent(Student student)
50+
public Student createStudent(Student student)
5151
{
5252
SqlSession sqlSession = openSqlSession();
5353
try {
5454
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
5555
studentMapper.insertStudent(student);
5656
sqlSession.commit();
57+
return student;
5758
} finally {
5859
sqlSession.close();
5960
}

chapter02/src/main/resources/com/mybatis3/mappers/StudentMapper.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<mapper namespace="com.mybatis3.mappers.StudentMapper">
77

88
<resultMap type="Student" id="StudentResult">
9-
<id property="id" column="student_id"/>
9+
<id property="studId" column="stud_id"/>
1010
<result property="name" column="name"/>
1111
<result property="email" column="email"/>
1212
<result property="dob" column="dob"/>
@@ -17,15 +17,15 @@
1717
</select>
1818

1919
<select id="findStudentById" parameterType="int" resultType="Student">
20-
select stud_id as id, name, email, dob from Students where stud_id=#{Id}
20+
select stud_id as studId, name, email, dob from Students where stud_id=#{studId}
2121
</select>
2222

23-
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
23+
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="studId">
2424
INSERT INTO STUDENTS(NAME,EMAIL,DOB) VALUES(#{name},#{email},#{dob})
2525
</insert>
2626

2727
<update id="updateStudent" parameterType="Student">
28-
UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{id}
28+
UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{studId}
2929
</update>
3030

3131
</mapper>

chapter02/src/test/java/com/mybatis3/services/StudentServiceTest.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.mybatis3.services;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
36
import java.util.Date;
47
import java.util.List;
58

69
import org.apache.ibatis.session.SqlSessionFactory;
710
import org.junit.AfterClass;
8-
import org.junit.Assert;
911
import org.junit.BeforeClass;
1012
import org.junit.Test;
1113

@@ -19,9 +21,12 @@ public class StudentServiceTest
1921
@BeforeClass
2022
public static void setup()
2123
{
24+
TestDataPopulator.initDatabase();
25+
2226
SqlSessionFactory sqlSessionFactory = null;
2327
//Use this if you want XML based configuration
2428
sqlSessionFactory = MyBatisUtil.getSqlSessionFactoryUsingXML();
29+
2530
//Use this if you want to use Java API configuration
2631
//sqlSessionFactory = MyBatisUtil.getSqlSessionFactoryUsingJavaAPI();
2732
studentService = new StudentService(sqlSessionFactory);
@@ -37,10 +42,11 @@ public static void teardown()
3742
public void testFindAllStudents()
3843
{
3944
List<Student> students = studentService.findAllStudents();
40-
Assert.assertNotNull(students);
45+
assertNotNull(students);
4146
for (Student student : students)
4247
{
43-
System.out.println(student);
48+
assertNotNull(student);
49+
//System.out.println(student);
4450
}
4551

4652
}
@@ -49,35 +55,39 @@ public void testFindAllStudents()
4955
public void testFindStudentById()
5056
{
5157
Student student = studentService.findStudentById(1);
52-
Assert.assertNotNull(student);
53-
System.out.println(student);
58+
assertNotNull(student);
5459
}
5560

5661
@Test
5762
public void testCreateUStudent()
5863
{
5964
Student student = new Student();
60-
long id = System.currentTimeMillis();
65+
int id = 4;
66+
student.setStudId(id);
6167
student.setName("student_"+id);
6268
student.setEmail("student_"+id+"gmail.com");
6369
student.setDob(new Date());
64-
studentService.createStudent(student);
65-
Student newStudent = studentService.findStudentById(student.getId());
66-
Assert.assertNotNull(newStudent);
70+
Student newStudent = studentService.createStudent(student);
71+
assertNotNull(newStudent);
72+
assertEquals("student_"+id, newStudent.getName());
73+
assertEquals("student_"+id+"gmail.com", newStudent.getEmail());
6774
}
6875

69-
@Test
76+
@Test
7077
public void testUpdateStudent()
7178
{
72-
int id = 1;
79+
int id = 2;
7380
Student student =studentService.findStudentById(id);
74-
student.setId(id);
81+
student.setStudId(id);
7582
student.setName("student_"+id);
7683
student.setEmail("student_"+id+"gmail.com");
7784
Date now = new Date();
7885
student.setDob(now);
7986
studentService.updateStudent(student);
8087
Student updatedStudent = studentService.findStudentById(id);
81-
Assert.assertNotNull(updatedStudent);
88+
assertNotNull(updatedStudent);
89+
assertEquals("student_"+id, updatedStudent.getName());
90+
assertEquals("student_"+id+"gmail.com", updatedStudent.getEmail());
91+
8292
}
8393
}

chapter03/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
66
<modelVersion>4.0.0</modelVersion>
77

8-
<groupId>com.mybatisdemo</groupId>
9-
<version>0.0.1</version>
8+
<groupId>com.mybatis3</groupId>
109
<artifactId>chapter03</artifactId>
10+
<version>0.0.1</version>
1111
<name>chapter03</name>
1212
<url>http://www.mybatis.org</url>
1313
<description>MyBatis Book Chapter 03</description>

0 commit comments

Comments
 (0)