Skip to content

Commit 3140ea1

Browse files
bs-santoshadamd1985
authored andcommitted
Bs santosh spring mybatis (eugenp#1479)
* Spring and MyBatis integration maven project Complete article is available in http://inprogress.baeldung.com/wp-admin/post.php * Spring-MyBatis integration example This code demonstrates how to use MyBatis in Sring environment. Full details could be found in article http://inprogress.baeldung.com/?p=31706&preview=true
1 parent 319dd26 commit 3140ea1

17 files changed

Lines changed: 753 additions & 0 deletions

File tree

‎spring-mybatis/pom.xml‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-mybatis</artifactId>
6+
<packaging>jar</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>spring-mybatis Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.mybatis</groupId>
13+
<artifactId>mybatis</artifactId>
14+
<version>3.1.1</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.mybatis</groupId>
18+
<artifactId>mybatis-spring</artifactId>
19+
<version>1.1.1</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-context-support</artifactId>
24+
<version>3.1.1.RELEASE</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-test</artifactId>
29+
<version>3.1.1.RELEASE</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<version>5.1.40</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.servlet</groupId>
39+
<artifactId>jstl</artifactId>
40+
<version>1.2</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework</groupId>
44+
<artifactId>spring-webmvc</artifactId>
45+
<version>3.2.4.RELEASE</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>javax.servlet</groupId>
49+
<artifactId>servlet-api</artifactId>
50+
<version>2.5</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<version>3.8.1</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
<build>
60+
<finalName>spring-mybatis</finalName>
61+
</build>
62+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.spring.mybatis.application;
2+
3+
import java.util.Date;
4+
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
import com.baeldung.spring.mybatis.model.Student;
8+
import com.baeldung.spring.mybatis.service.StudentService;
9+
10+
public class SpringMyBatisApplication {
11+
12+
public static void main(String[] args){
13+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("mybatis-spring.xml");
14+
15+
StudentService studentService = (StudentService) ctx.getBean("studentService");
16+
Student student = new Student();
17+
student.setFirstName("Santosh");
18+
student.setLastName("B S");
19+
student.setEmailAddress("santosh.bse@gmail.com");
20+
student.setPassword("Test123");
21+
student.setDateOfBirth(new Date());
22+
student.setUserName("santoshbs1");
23+
24+
boolean result = studentService.insertStudent(student);
25+
if(result){
26+
System.out.println("Student record saved successfully");
27+
}
28+
else{
29+
System.out.println("Encountered an error while saving student data");
30+
}
31+
32+
final String userName = "santosh";
33+
Student matchingStudent = studentService.getStudentByUserName(userName);
34+
if(matchingStudent == null){
35+
System.out.println("No matching student found for User Name - " + userName);
36+
}
37+
else{
38+
System.out.println("Student Details are as follows : ");
39+
System.out.println("First Name : " + matchingStudent.getFirstName());
40+
System.out.println("Last Name : " + matchingStudent.getLastName());
41+
System.out.println("EMail : " + matchingStudent.getEmailAddress());
42+
System.out.println("DOB : " + matchingStudent.getDateOfBirth());
43+
System.out.println("User Name : " + matchingStudent.getUserName());
44+
}
45+
46+
}
47+
48+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.spring.mybatis.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.ui.ModelMap;
7+
import org.springframework.validation.BindingResult;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.SessionAttributes;
13+
14+
import com.baeldung.spring.mybatis.model.Student;
15+
import com.baeldung.spring.mybatis.model.StudentLogin;
16+
import com.baeldung.spring.mybatis.service.StudentService;
17+
18+
@Controller
19+
@SessionAttributes("student")
20+
public class StudentController {
21+
22+
@Autowired
23+
private StudentService studentService;
24+
25+
@RequestMapping(value = "/signup", method = RequestMethod.GET)
26+
public String signup(Model model) {
27+
Student student = new Student();
28+
model.addAttribute("student", student);
29+
return "signup";
30+
}
31+
32+
@RequestMapping(value = "/signup", method = RequestMethod.POST)
33+
public String signup(@Validated @ModelAttribute("student") Student student, BindingResult result, ModelMap model) {
34+
if (studentService.getStudentByUserName(student.getUserName())) {
35+
model.addAttribute("message", "User Name exists. Try another user name");
36+
return "signup";
37+
} else {
38+
studentService.insertStudent(student);
39+
model.addAttribute("message", "Saved student details");
40+
return "redirect:login.html";
41+
}
42+
}
43+
44+
@RequestMapping(value = "/login", method = RequestMethod.GET)
45+
public String login(Model model) {
46+
StudentLogin studentLogin = new StudentLogin();
47+
model.addAttribute("studentLogin", studentLogin);
48+
return "login";
49+
}
50+
51+
@RequestMapping(value = "/login", method = RequestMethod.POST)
52+
public String login(@ModelAttribute("studentLogin") StudentLogin studentLogin, BindingResult result, ModelMap model) {
53+
boolean found = studentService.getStudentByLogin(studentLogin.getUserName(), studentLogin.getPassword());
54+
if (found) {
55+
return "success";
56+
} else {
57+
return "failure";
58+
}
59+
}
60+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring.mybatis.mappers;
2+
3+
import org.apache.ibatis.annotations.Insert;
4+
import org.apache.ibatis.annotations.Options;
5+
import org.apache.ibatis.annotations.Select;
6+
7+
import com.baeldung.spring.mybatis.model.Student;
8+
9+
public interface StudentMapper {
10+
@Insert("INSERT INTO student(userName, password, firstName,lastName, dateOfBirth, emailAddress) VALUES"
11+
+ "(#{userName},#{password}, #{firstName}, #{lastName}, #{dateOfBirth}, #{emailAddress})")
12+
@Options(useGeneratedKeys = true, keyProperty = "id", flushCache = true, keyColumn = "id")
13+
public void insertStudent(Student student);
14+
15+
@Select("SELECT USERNAME as userName, PASSWORD as password, FIRSTNAME as firstName, LASTNAME as lastName, "
16+
+ "DATEOFBIRTH as dateOfBirth, EMAILADDRESS as emailAddress " + "FROM student WHERE userName = #{userName}")
17+
public Student getStudentByUserName(String userName);
18+
19+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.spring.mybatis.model;
2+
3+
import java.util.Date;
4+
5+
public class Student {
6+
private Long id;
7+
private String userName;
8+
private String firstName;
9+
private String lastName;
10+
private String password;
11+
private String emailAddress;
12+
private Date dateOfBirth;
13+
public Long getId() {
14+
return id;
15+
}
16+
public void setId(Long id) {
17+
this.id = id;
18+
}
19+
public String getUserName() {
20+
return userName;
21+
}
22+
public void setUserName(String userName) {
23+
this.userName = userName;
24+
}
25+
public String getFirstName() {
26+
return firstName;
27+
}
28+
public void setFirstName(String firstName) {
29+
this.firstName = firstName;
30+
}
31+
public String getLastName() {
32+
return lastName;
33+
}
34+
public void setLastName(String lastName) {
35+
this.lastName = lastName;
36+
}
37+
public String getPassword() {
38+
return password;
39+
}
40+
public void setPassword(String password) {
41+
this.password = password;
42+
}
43+
public String getEmailAddress() {
44+
return emailAddress;
45+
}
46+
public void setEmailAddress(String emailAddress) {
47+
this.emailAddress = emailAddress;
48+
}
49+
public Date getDateOfBirth() {
50+
return dateOfBirth;
51+
}
52+
public void setDateOfBirth(Date dateOfBirth) {
53+
this.dateOfBirth = dateOfBirth;
54+
}
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.spring.mybatis.model;
2+
3+
public class StudentLogin {
4+
private String userName;
5+
private String password;
6+
public String getUserName() {
7+
return userName;
8+
}
9+
public void setUserName(String userName) {
10+
this.userName = userName;
11+
}
12+
public String getPassword() {
13+
return password;
14+
}
15+
public void setPassword(String password) {
16+
this.password = password;
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.spring.mybatis.service;
2+
3+
import com.baeldung.spring.mybatis.model.Student;
4+
5+
public interface StudentService {
6+
public boolean insertStudent(Student student);
7+
public Student getStudentByLogin(String userName, String password);
8+
public Student getStudentByUserName(String userName);
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.spring.mybatis.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
import com.baeldung.spring.mybatis.mappers.StudentMapper;
8+
import com.baeldung.spring.mybatis.model.Student;
9+
10+
@Service("studentService")
11+
public class StudentServiceImpl implements StudentService {
12+
13+
@Autowired
14+
private StudentMapper studentMapper;
15+
16+
@Transactional
17+
public boolean insertStudent(Student student) {
18+
boolean result=false;
19+
try{
20+
studentMapper.insertStudent(student);
21+
result = true;
22+
}
23+
catch(Exception ex){
24+
ex.printStackTrace();
25+
result = false;
26+
}
27+
return result;
28+
}
29+
30+
public Student getStudentByLogin(String userName, String password) {
31+
Student student = studentMapper.getStudentByUserName(userName);
32+
return student;
33+
}
34+
35+
public Student getStudentByUserName(String userName) {
36+
Student student = studentMapper.getStudentByUserName(userName);
37+
return student;
38+
}
39+
40+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
5+
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
8+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
9+
10+
<mvc:annotation-driven />
11+
<context:component-scan base-package="com.baeldung.spring.mybatis" />
12+
<bean
13+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14+
<property name="prefix" value="/WEB-INF/jsp/" />
15+
<property name="suffix" value=".jsp" />
16+
</bean>
17+
<!-- <bean id="dataSource"
18+
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19+
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
20+
<property name="url" value="jdbc:mysql//localhost:3306/baeldung;" />
21+
<property name="username" value="root" />
22+
<property name="password" value="admin" />
23+
</bean> -->
24+
25+
26+
<bean id="dataSource"
27+
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
28+
29+
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
30+
<property name="url" value="jdbc:mysql://localhost:3306/baeldung" />
31+
<property name="username" value="root" />
32+
<property name="password" value="admin" />
33+
</bean>
34+
35+
<tx:annotation-driven transaction-manager="transactionManager" />
36+
<bean id="transactionManager"
37+
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
38+
<property name="dataSource" ref="dataSource" />
39+
</bean>
40+
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
41+
<property name="dataSource" ref="dataSource" />
42+
<property name="typeAliasesPackage" value="com.baeldung.spring.mybatis.model" />
43+
<property name="mapperLocations" value="classpath*:com/baeldung/spring/mappers/*.xml" />
44+
</bean>
45+
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
46+
<constructor-arg index="0" ref="sqlSessionFactory" />
47+
</bean>
48+
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
49+
<property name="basePackage" value="com.baeldung.spring.mybatis.mappers" />
50+
</bean>
51+
52+
<bean id="studentService" class="com.baeldung.spring.mybatis.service.StudentServiceImpl"></bean>
53+
54+
</beans>

0 commit comments

Comments
 (0)