Skip to content

Commit 9aa9c38

Browse files
Latest chapter01 code merged.
1 parent 67da960 commit 9aa9c38

File tree

9 files changed

+18
-46
lines changed

9 files changed

+18
-46
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.22</mysql.version>
24+
<mysql.version>5.1.18</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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public interface StudentMapper
1616

1717
Student findStudentById(Integer id);
1818

19-
Student findStudentByEmail(String email);
20-
2119
void insertStudent(Student student);
2220

2321
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";
22+
private static final String URL = "jdbc:mysql://localhost:3306/elearning";
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 elearning.students where stud_id=?";
55+
String sql = "select * from 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: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,9 @@ 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);
3938
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
4039
return 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);
40+
//return sqlSession.selectOne("com.mybatis3.StudentMapper.findStudentById", studId);
5241
} finally {
5342
sqlSession.close();
5443
}

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

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

19-
private static SqlSessionFactory initSqlSessionFactory() {
20-
InputStream inputStream;
19+
public static SqlSessionFactory getSqlSessionFactory()
20+
{
21+
if(sqlSessionFactory==null)
22+
{
23+
InputStream inputStream;
2124
try
2225
{
2326
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
@@ -26,14 +29,12 @@ private static SqlSessionFactory initSqlSessionFactory() {
2629
{
2730
throw new RuntimeException(e.getCause());
2831
}
32+
}
2933
return sqlSessionFactory;
3034
}
3135

32-
public static SqlSessionFactory getSqlSessionFactory() {
33-
return sqlSessionFactory;
34-
}
35-
36-
public static SqlSession getSqlSession() {
36+
public static SqlSession getSqlSession()
37+
{
3738
return getSqlSessionFactory().openSession();
3839
}
3940
}

chapter01/src/main/resources/application.properties

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

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

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

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

1919
<select id="findStudentById" parameterType="int" resultType="Student">
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}
20+
select stud_id as studId, name, email, dob from Students where stud_id=#{studId}
2521
</select>
2622

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

chapter01/src/main/resources/mybatis-config.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<properties resource="application.properties"/>
88

99
<typeAliases>
10-
<typeAlias alias="Student" type="com.mybatis3.domain.Student"/>
1110
<package name="com.mybatis3.domain"/>
1211
</typeAliases>
1312

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

Lines changed: 2 additions & 10 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,14 +52,6 @@ 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-
6355
@Test
6456
@Ignore
6557
public void testCreateUStudent()
@@ -76,7 +68,7 @@ public void testCreateUStudent()
7668
}
7769

7870
@Test
79-
@Ignore
71+
//@Ignore
8072
public void testUpdateStudent()
8173
{
8274
int id = 3;

0 commit comments

Comments
 (0)