Skip to content

Commit 04b2d2e

Browse files
Adding Chapter02 module.
1 parent 1bb0709 commit 04b2d2e

File tree

11 files changed

+402
-2
lines changed

11 files changed

+402
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3+
http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<parent>
9+
<artifactId>mybatisdemo-parent</artifactId>
10+
<groupId>com.mybatisdemo</groupId>
11+
<version>0.0.1</version>
12+
</parent>
13+
14+
<artifactId>chapter02</artifactId>
15+
<name>chapter02</name>
16+
<url>http://maven.apache.org</url>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
24+
</dependencies>
25+
26+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package chapter02;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.apache.ibatis.datasource.pooled.PooledDataSource;
7+
import org.apache.ibatis.io.Resources;
8+
import org.apache.ibatis.mapping.Environment;
9+
import org.apache.ibatis.session.Configuration;
10+
import org.apache.ibatis.session.SqlSessionFactory;
11+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
12+
import org.apache.ibatis.transaction.TransactionFactory;
13+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
14+
15+
/**
16+
* @author Siva
17+
*
18+
*/
19+
public class MyBatisUtil
20+
{
21+
private static SqlSessionFactory xmlSqlSessionFactory;
22+
private static SqlSessionFactory javaSqlSessionFactory;
23+
24+
public static SqlSessionFactory getSqlSessionFactoryUsingXML()
25+
{
26+
if(xmlSqlSessionFactory==null)
27+
{
28+
InputStream inputStream;
29+
try
30+
{
31+
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
32+
xmlSqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
33+
}catch (IOException e)
34+
{
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
return xmlSqlSessionFactory;
39+
}
40+
41+
public static SqlSessionFactory getSqlSessionFactoryUsingJavaAPI()
42+
{
43+
if(javaSqlSessionFactory==null)
44+
{
45+
try
46+
{
47+
String driver = "com.mysql.jdbc.Driver";
48+
String url = "jdbc:mysql://localhost:3306/mybatisdemo";
49+
String username = "root";
50+
String password = "admin";
51+
PooledDataSource dataSource = new PooledDataSource(driver, url, username, password);
52+
TransactionFactory transactionFactory = new JdbcTransactionFactory();
53+
Environment environment = new Environment("development", transactionFactory, dataSource);
54+
Configuration configuration = new Configuration(environment);
55+
configuration.getTypeAliasRegistry().registerAliases("chapter02");
56+
configuration.addMapper(StudentMapper.class);
57+
javaSqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
58+
59+
}catch (Exception e)
60+
{
61+
throw new RuntimeException(e);
62+
}
63+
}
64+
return javaSqlSessionFactory;
65+
}
66+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package chapter02;
2+
3+
import java.util.Date;
4+
5+
6+
/**
7+
* @author Siva
8+
*
9+
*/
10+
public class Student
11+
{
12+
private Integer id;
13+
private String name;
14+
private String email;
15+
private Date dob;
16+
17+
18+
@Override
19+
public String toString()
20+
{
21+
return "Student [id=" + id + ", name=" + name + ", email=" + email
22+
+ ", dob=" + dob + "]";
23+
}
24+
public Student()
25+
{
26+
}
27+
public Student(Integer id)
28+
{
29+
this.id = id;
30+
}
31+
public Integer getId()
32+
{
33+
return id;
34+
}
35+
public void setId(Integer id)
36+
{
37+
this.id = 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 Date getDob()
56+
{
57+
return dob;
58+
}
59+
public void setDob(Date dob)
60+
{
61+
this.dob = dob;
62+
}
63+
64+
65+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chapter02;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Siva
7+
*
8+
*/
9+
public interface StudentMapper
10+
{
11+
12+
List<Student> findAllStudents();
13+
14+
Student findStudentById(Integer id);
15+
16+
void insertStudent(Student student);
17+
18+
void updateStudent(Student student);
19+
20+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package chapter02;
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+
public class StudentService
10+
{
11+
private Logger logger = LoggerFactory.getLogger(getClass());
12+
13+
protected SqlSession openSqlSession()
14+
{
15+
//return MyBatisUtil.getSqlSessionFactoryUsingXML().openSession();
16+
return MyBatisUtil.getSqlSessionFactoryUsingJavaAPI().openSession();
17+
}
18+
public List<Student> findAllStudents()
19+
{
20+
SqlSession sqlSession = openSqlSession();
21+
try {
22+
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
23+
return studentMapper.findAllStudents();
24+
} finally {
25+
sqlSession.close();
26+
}
27+
}
28+
29+
public Student findStudentById(Integer studId)
30+
{
31+
logger.debug("Select Student By ID :{}", studId);
32+
SqlSession sqlSession = openSqlSession();
33+
try {
34+
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
35+
return studentMapper.findStudentById(studId);
36+
//return sqlSession.selectOne("chapter01.StudentMapper.findStudentById", studId);
37+
} finally {
38+
sqlSession.close();
39+
}
40+
}
41+
42+
public void createStudent(Student student)
43+
{
44+
SqlSession sqlSession = openSqlSession();
45+
try {
46+
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
47+
studentMapper.insertStudent(student);
48+
sqlSession.commit();
49+
} finally {
50+
sqlSession.close();
51+
}
52+
}
53+
54+
public void updateStudent(Student student)
55+
{
56+
SqlSession sqlSession = openSqlSession();
57+
try {
58+
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
59+
studentMapper.updateStudent(student);
60+
sqlSession.commit();
61+
} finally {
62+
sqlSession.close();
63+
}
64+
}
65+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
################### DataSource Configuration ##########################
4+
5+
jdbc.driverClassName=com.mysql.jdbc.Driver
6+
jdbc.url=jdbc:mysql://localhost:3306/mybatisdemo
7+
jdbc.username=root
8+
jdbc.password=admin
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
6+
<mapper namespace="chapter02.StudentMapper">
7+
8+
<resultMap type="Student" id="StudentResult">
9+
<id property="id" column="student_id"/>
10+
<result property="name" column="name"/>
11+
<result property="email" column="email"/>
12+
<result property="dob" column="dob"/>
13+
</resultMap>
14+
15+
<select id="findAllStudents" resultMap="StudentResult">
16+
select * from Students
17+
</select>
18+
19+
<select id="findStudentById" parameterType="int" resultType="Student">
20+
select stud_id as id, name, email, dob from Students where stud_id=#{Id}
21+
</select>
22+
23+
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
24+
INSERT INTO STUDENTS(NAME,EMAIL,DOB) VALUES(#{name},#{email},#{dob})
25+
</insert>
26+
27+
<update id="updateStudent" parameterType="Student">
28+
UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{id}
29+
</update>
30+
31+
</mapper>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
<configuration>
6+
7+
<properties resource="application.properties"/>
8+
9+
<typeAliases>
10+
<package name="chapter02"/>
11+
</typeAliases>
12+
13+
<environments default="development">
14+
<environment id="development">
15+
<transactionManager type="JDBC"/>
16+
<dataSource type="POOLED">
17+
<property name="driver" value="${jdbc.driverClassName}"/>
18+
<property name="url" value="${jdbc.url}"/>
19+
<property name="username" value="${jdbc.username}"/>
20+
<property name="password" value="${jdbc.password}"/>
21+
</dataSource>
22+
</environment>
23+
</environments>
24+
25+
<mappers>
26+
<mapper resource="chapter02/StudentMapper.xml"/>
27+
</mappers>
28+
29+
30+
</configuration>

0 commit comments

Comments
 (0)