Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions spring-mybatis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-mybatis</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mybatis Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-mybatis</finalName>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.spring.mybatis.application;

import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.baeldung.spring.mybatis.model.Student;
import com.baeldung.spring.mybatis.service.StudentService;

public class SpringMyBatisApplication {

public static void main(String[] args){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("mybatis-spring.xml");

StudentService studentService = (StudentService) ctx.getBean("studentService");
Student student = new Student();
student.setFirstName("Santosh");
student.setLastName("B S");
student.setEmailAddress("santosh.bse@gmail.com");
student.setPassword("Test123");
student.setDateOfBirth(new Date());
student.setUserName("santoshbs1");

boolean result = studentService.insertStudent(student);
if(result){
System.out.println("Student record saved successfully");
}
else{
System.out.println("Encountered an error while saving student data");
}

final String userName = "santosh";
Student matchingStudent = studentService.getStudentByUserName(userName);
if(matchingStudent == null){
System.out.println("No matching student found for User Name - " + userName);
}
else{
System.out.println("Student Details are as follows : ");
System.out.println("First Name : " + matchingStudent.getFirstName());
System.out.println("Last Name : " + matchingStudent.getLastName());
System.out.println("EMail : " + matchingStudent.getEmailAddress());
System.out.println("DOB : " + matchingStudent.getDateOfBirth());
System.out.println("User Name : " + matchingStudent.getUserName());
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.baeldung.spring.mybatis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.baeldung.spring.mybatis.model.Student;
import com.baeldung.spring.mybatis.model.StudentLogin;
import com.baeldung.spring.mybatis.service.StudentService;

@Controller
@SessionAttributes("student")
public class StudentController {

@Autowired
private StudentService studentService;

@RequestMapping(value = "/signup", method = RequestMethod.GET)
public String signup(Model model) {
Student student = new Student();
model.addAttribute("student", student);
return "signup";
}

@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String signup(@Validated @ModelAttribute("student") Student student, BindingResult result, ModelMap model) {
if (studentService.getStudentByUserName(student.getUserName())) {
model.addAttribute("message", "User Name exists. Try another user name");
return "signup";
} else {
studentService.insertStudent(student);
model.addAttribute("message", "Saved student details");
return "redirect:login.html";
}
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
StudentLogin studentLogin = new StudentLogin();
model.addAttribute("studentLogin", studentLogin);
return "login";
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("studentLogin") StudentLogin studentLogin, BindingResult result, ModelMap model) {
boolean found = studentService.getStudentByLogin(studentLogin.getUserName(), studentLogin.getPassword());
if (found) {
return "success";
} else {
return "failure";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.spring.mybatis.mappers;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import com.baeldung.spring.mybatis.model.Student;

public interface StudentMapper {
@Insert("INSERT INTO student(userName, password, firstName,lastName, dateOfBirth, emailAddress) VALUES"
+ "(#{userName},#{password}, #{firstName}, #{lastName}, #{dateOfBirth}, #{emailAddress})")
@Options(useGeneratedKeys = true, keyProperty = "id", flushCache = true, keyColumn = "id")
public void insertStudent(Student student);

@Select("SELECT USERNAME as userName, PASSWORD as password, FIRSTNAME as firstName, LASTNAME as lastName, "
+ "DATEOFBIRTH as dateOfBirth, EMAILADDRESS as emailAddress " + "FROM student WHERE userName = #{userName}")
public Student getStudentByUserName(String userName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.spring.mybatis.model;

import java.util.Date;

public class Student {
private Long id;
private String userName;
private String firstName;
private String lastName;
private String password;
private String emailAddress;
private Date dateOfBirth;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.spring.mybatis.model;

public class StudentLogin {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.spring.mybatis.service;

import com.baeldung.spring.mybatis.model.Student;

public interface StudentService {
public boolean insertStudent(Student student);
public Student getStudentByLogin(String userName, String password);
public Student getStudentByUserName(String userName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.baeldung.spring.mybatis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baeldung.spring.mybatis.mappers.StudentMapper;
import com.baeldung.spring.mybatis.model.Student;

@Service("studentService")
public class StudentServiceImpl implements StudentService {

@Autowired
private StudentMapper studentMapper;

@Transactional
public boolean insertStudent(Student student) {
boolean result=false;
try{
studentMapper.insertStudent(student);
result = true;
}
catch(Exception ex){
ex.printStackTrace();
result = false;
}
return result;
}

public Student getStudentByLogin(String userName, String password) {
Student student = studentMapper.getStudentByUserName(userName);
return student;
}

public Student getStudentByUserName(String userName) {
Student student = studentMapper.getStudentByUserName(userName);
return student;
}

}
54 changes: 54 additions & 0 deletions spring-mybatis/src/main/resources/mybatis-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.baeldung.spring.mybatis" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql//localhost:3306/baeldung;" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean> -->


<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/baeldung" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.baeldung.spring.mybatis.model" />
<property name="mapperLocations" value="classpath*:com/baeldung/spring/mappers/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.baeldung.spring.mybatis.mappers" />
</bean>

<bean id="studentService" class="com.baeldung.spring.mybatis.service.StudentServiceImpl"></bean>

</beans>
Loading