Skip to content

Commit 5e1351a

Browse files
Module refactoring.
1 parent 7ecb6ba commit 5e1351a

File tree

112 files changed

+3205
-1305
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

+3205
-1305
lines changed

chapter01/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<logback.version>1.0.1</logback.version>
2222
<log4j.version>1.2.16</log4j.version>
2323
<mybatis.version>3.1.1</mybatis.version>
24-
<mysql.version>5.1.18</mysql.version>
24+
<mysql.version>5.1.22</mysql.version>
2525
<maven.compiler.plugin>2.3.2</maven.compiler.plugin>
2626
</properties>
2727

chapter01/src/main/java/com/mybatis3/mappers/StudentMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public interface StudentMapper
1616

1717
Student findStudentById(Integer id);
1818

19+
Student findStudentByEmail(String email);
20+
1921
void insertStudent(Student student);
2022

2123
void updateStudent(Student student);

chapter01/src/main/java/com/mybatis3/services/JdbcStudentService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class JdbcStudentService
1919
{
2020

2121
private static final String DRIVER = "com.mysql.jdbc.Driver";
22-
private static final String URL = "jdbc:mysql://localhost:3306/elearning";
22+
private static final String URL = "jdbc:mysql://localhost:3306";
2323
private static final String USERNAME = "root";
2424
private static final String PASSWORD = "admin";
2525

@@ -32,7 +32,7 @@ public static void main(String[] args)
3232
System.out.println(existingStudent);
3333

3434

35-
long ts = System.currentTimeMillis();//For creating unique student names
35+
/*long ts = System.currentTimeMillis();//For creating unique student names
3636
Student newStudent = new Student(0,"student_"+ts,"student_"+ts+"@gmail.com",new Date());
3737
service.createStudent(newStudent);
3838
System.out.println(newStudent);
@@ -42,7 +42,7 @@ public static void main(String[] args)
4242
ts = System.currentTimeMillis();//For creating unique student email
4343
updateStudent.setEmail("student_"+ts+"@gmail.com");
4444
service.updateStudent(updateStudent);
45-
45+
*/
4646
}
4747

4848
public Student findStudentById(int studId)
@@ -52,7 +52,7 @@ public Student findStudentById(int studId)
5252
try
5353
{
5454
conn = getDatabaseConnection();
55-
String sql = "select * from students where stud_id=?";
55+
String sql = "select * from elearning.students where stud_id=?";
5656
PreparedStatement pstmt = conn.prepareStatement(sql);
5757
pstmt.setInt(1, studId);
5858
ResultSet rs = pstmt.executeQuery();

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ public Student findStudentById(Integer studId)
3535
logger.debug("Select Student By ID :{}", studId);
3636
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
3737
try {
38+
//Student student = sqlSession.selectOne("com.mybatis3.StudentMapper.findStudentById", studId);
3839
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
3940
return studentMapper.findStudentById(studId);
40-
//return sqlSession.selectOne("com.mybatis3.StudentMapper.findStudentById", studId);
41+
} finally {
42+
sqlSession.close();
43+
}
44+
}
45+
46+
public Student findStudentByEmail(String email)
47+
{
48+
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession();
49+
try {
50+
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
51+
return studentMapper.findStudentByEmail(email);
4152
} finally {
4253
sqlSession.close();
4354
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
*/
1515
public class MyBatisSqlSessionFactory
1616
{
17-
private static SqlSessionFactory sqlSessionFactory;
17+
private static SqlSessionFactory sqlSessionFactory = initSqlSessionFactory();
1818

19-
public static SqlSessionFactory getSqlSessionFactory()
20-
{
21-
if(sqlSessionFactory==null)
22-
{
23-
InputStream inputStream;
19+
private static SqlSessionFactory initSqlSessionFactory() {
20+
InputStream inputStream;
2421
try
2522
{
2623
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
@@ -29,12 +26,14 @@ public static SqlSessionFactory getSqlSessionFactory()
2926
{
3027
throw new RuntimeException(e.getCause());
3128
}
32-
}
3329
return sqlSessionFactory;
3430
}
3531

36-
public static SqlSession getSqlSession()
37-
{
32+
public static SqlSessionFactory getSqlSessionFactory() {
33+
return sqlSessionFactory;
34+
}
35+
36+
public static SqlSession getSqlSession() {
3837
return getSqlSessionFactory().openSession();
3938
}
4039
}

chapter01/src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44

55
jdbc.driverClassName=com.mysql.jdbc.Driver
66
jdbc.url=jdbc:mysql://localhost:3306/elearning
7+
#jdbc.url=jdbc:mysql://localhost:3306
78
jdbc.username=root
89
jdbc.password=admin
10+
11+
schemaname=elearning

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
</select>
1818

1919
<select id="findStudentById" parameterType="int" resultType="Student">
20-
select stud_id as studId, name, email, dob from Students where stud_id=#{studId}
20+
select stud_id as studId, name, email, dob from ${schemaname}.Students where stud_id=#{studId}
21+
</select>
22+
23+
<select id="findStudentByEmail" parameterType="String" resultType="Student">
24+
select stud_id as studId, name, email, dob from Students where email=#{email}
2125
</select>
2226

2327
<insert id="insertStudent" parameterType="Student">

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,13 @@ CREATE TABLE COURSE_ENROLLMENT
6767

6868

6969

70+
CREATE TABLE USER_PICS
71+
(
72+
ID INT(11) NOT NULL AUTO_INCREMENT,
73+
NAME VARCHAR(50) DEFAULT NULL,
74+
PIC BLOB,
75+
BIO LONGTEXT,
76+
PRIMARY KEY (ID)
77+
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=LATIN1;
7078

7179

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void teardown()
3232
studentService = null;
3333
}
3434

35-
@Test
35+
//@Test
3636
public void testFindAllStudents()
3737
{
3838
List<Student> students = studentService.findAllStudents();
@@ -52,6 +52,14 @@ public void testFindStudentById()
5252
System.out.println(student);
5353
}
5454

55+
//@Test
56+
public void testFindStudentByEmail()
57+
{
58+
Student student = studentService.findStudentByEmail("student_1gmail.com");
59+
Assert.assertNotNull(student);
60+
System.out.println(student);
61+
}
62+
5563
@Test
5664
@Ignore
5765
public void testCreateUStudent()
@@ -68,7 +76,7 @@ public void testCreateUStudent()
6876
}
6977

7078
@Test
71-
//@Ignore
79+
@Ignore
7280
public void testUpdateStudent()
7381
{
7482
int id = 3;

chapter02/README.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Chapter 2: Bootstrapping MyBatis
2-
================================
3-
This chapter describes the approaches to configure and bootstrap MyBatis.
4-
Topics covered:
2+
=======================================
3+
This module, chapter02, is a maven based java project to demonstrate the following approaches to configure and bootstrap MyBatis.
54
. Configuration using XML
6-
. Configuration using Java API.
5+
. Configuration using Java API.
6+
7+
Note: You can create MySQL Database tables using scripts in src/main/resources/sql folder of chapter01 module.
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+

0 commit comments

Comments
 (0)