spring + mybatis

关注微信公众号:
huyouxiao.com
回复语言名称,比如java,python,go,C, C++.有海量资源免费赠送!

Building Java Web Application Using MyBatis With Spring

This post will show how to create a Student Enrollment Application using MYSQL DB with MyBatis framework in a Spring environment. This is a simple application that aims to collect the input details from the user during signup, save the details in the MYSQL DB and authenticate the same during login.

1. Create Java Web Application Project using Maven Template

To begin with, in the IDE, create a Java Maven project with the template of maven-archetype-webapp (Filter the catalog based on the string “webapp”) by providing appropriate values for GroupId and Artifact Id for the project. The sample web application directory structure is shown below with a standard deployment descriptor web.xml and Maven pom.xml

2. Update pom.xml

To make the above Maven Java Web Application project support the MyBatis framework, add the following dependencies to the existing pom.xml

  • mybatis (for MyBatis support)
  • mybatis-spring (for MyBatis-Spring integration support)
  • jstl, spring-webmvc, servlet-api and spring-context-support (for Spring support)
  • spring-test (may be optional, needed if Spring-test support is needed)
  • mysql-connector-java (for MYSQL support)
<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.21</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>

##3. Modify web.xml

Modify the contents of the web.xml to include the following:

A servlet and specify the location of the configuration file for the same. In this sample, a configuration file named springConfig.xml is created under WEB-INF/config folder in the project layout.
A servlet-mapping to map the servlet created in the above step that should be invoked when the client specifies the url matching the url pattern.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
  <servlet-name>myBatisServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/springConfig.xml</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>myBatisServlet</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>

  <display-name>Archetype Created Web Application</display-name>
</web-app>

##4. Create the Spring Configuration File

Create a Spring Bean Configuration file under the folder WEB-INF/config. If STS(Spring Tool Suite) is the IDE, go ahead and enable the context, mvc and tx namespaces. The springConfig.xml will be as shown below

<?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">


</beans>

After enabling the required namespaces, include the following (in between the and tags) to indicate that the application is annotation driven and base package for the context component scan.

<mvc:annotation-driven />

<context:component-scan base-package="com.github.elizabetht" />

Include the bean InternalResourceViewResolver of Spring to locate the jsp files

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

Include the bean for data source, where the properties of the MYSQL DB like url, username and password can be specified. Replace with the actual connection url for connecting to the MYSQL DB. Likewise, replace and with the actual username and password values.

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql//<include connection url>:3306/studentEnrollment?autoReconnect=true&createDatabaseIfNotExist=true&" />
  <property name="username" value="<include username>" />
  <property name="password" value="<include password>" />
</bean>

Include the bean for transaction manager for scoping/controlling the transactions, that takes the data source defined above as reference (dependent)

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>

Coming to the MyBatis specific configurations, include the bean for sqlSessionFactory which is the central configuration in a MyBatis application. This bean takes in three properties – dataSource (already configured above) – typeAliasesPackage (location where the model classes of this application resides) – mapperLocations (location where the mapper xml files for the model resides – this is not needed here as annotation based configurations are used instead)

More details of this can be read at http://mybatis.github.io/mybatis-3/

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="typeAliasesPackage" value="com.github.elizabetht.model"/>
  <property name="mapperLocations" value="classpath*:com/github/elizabetht/mappers/*.xml" />
</bean>

Include the bean for sqlSession

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

Next and finally, include the bean for MapperScannerConfigurer

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.github.elizabetht.mappers" />
</bean>

##5. Create JSP Files for Student Signup/Login

Create a folder named “jsp” under WEB-INF (This is where the jsp files will be created as indicated in the springConfig.xml for the InternalResourceViewResolver bean).

Create a file signup.jsp to include a form to get the input details like UserName, Password, FirstName, LastName, DateOfBirth and EmailAddress of the student. A snapshot of the signup page is as follows:

MyBatis Signup Layout

Next, create a file login.jsp to include a form with UserName and Password. A snapshot of the login page is as follows:

MyBatis Login Layout

Also create success.jsp to indicate the login success and failure.jsp to indicate login failure (These are just pages used to display the contents – no processing logic involved).

This application uses twitter bootstrap http://getbootstrap.com/ and http://bootswatch.com/united/ as style sheets. It also uses a datepicker stylesheet as well to pop up a calendar for the DateOfBirth field in the Student Signup page (http://www.eyecon.ro/bootstrap-datepicker/).

A reference link to the files under webapp folder of this application can be found at https://github.com/elizabetht/StudentEnrollmentWithMyBatis/tree/master/src/main/webapp

6. Create packages for Controller, Model, Service and Mappers

Create packages each for the Spring Controller, Model and Service classes under the src/main/java folder. Also create a package for the MyBatis Mapper class under the same src/main/java folder.

A sample snapshot of the project after the package creation is as shown below:

MyBatis Package Layout

7. Create classes for Model Tier

Create a POJO class named Student.java inside the package com.github.elizabetht.model to include the details of the Student model entity during signup. Create another POJO class named StudentLogin.java inside the same package com.github.elizabetht.model to include the Student Login details.

A reference link to the files for the Model classes can be found at https://github.com/elizabetht/StudentEnrollmentWithMyBatis/tree/master/src/main/java/com/github/elizabetht/model

8. Create classes for MyBatis Mapper

A Mapper in MyBatis framework is similar to the Repository tier in a Spring environment. Crude SQL queries takes its place here. Create an interface class named StudentMapper.java inside the package com.github.elizabetht.mapper to support the database operations.

There are two interface methods needed for the application’s purpose.

  • To Insert the Student Signup details into the Database
  • To Verify the Student Login details from the Database
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);


}

##9. Create classes for Service Tier

Create an interface class named StudentService.java inside the package com.github.elizabetht.service to support the service tier operations.

public interface StudentService {
  void insertStudent(Student student);
  boolean getStudentByLogin(String userName, String password);
  boolean getStudentByUserName(String userName);
}

Create a service tier implementation class (a POJO indeed) named StudentServiceImpl.java inside the package com.github.elizabetht.service. This is where the application logic goes – either to save the student details into the database or to verify the student (already saved) details from the database.

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

  @Autowired
  private StudentMapper studentMapper;
  
  @Transactional
  public void insertStudent(Student student) {        
      studentMapper.insertStudent(student);
  }

  public boolean getStudentByLogin(String userName, String password) {        
      Student student = studentMapper.getStudentByUserName(userName);
      
      if(student != null && student.getPassword().equals(password)) {
          return true;
      }
      
      return false;
  }

  public boolean getStudentByUserName(String userName) {
      Student student = studentMapper.getStudentByUserName(userName);
      
      if(student != null) {
          return true;
      }
      
      return false;
  }

}
When using MyBatis with Spring, a mapper can be directly injected into the service tier. This is probably the strongest point of the Spring integration of MyBatis. This is the only tool that I am aware that lets to build the application with no imports to it.
@EduardoMacarron twitter.com/EduardoMacarron/…

10. Create class for Controller Tier

Create a Controller tier POJO class named StudentController.java inside the package com.github.elizabetht.controller. This is where the routing logic of the application goes – whether a signup or login action is called.

@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(@ModelAttribute("student") Student student, Model 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) {
      boolean found = studentService.getStudentByLogin(studentLogin.getUserName(), studentLogin.getPassword());
      if (found) {                
          return "success";
      } else {                
          return "failure";
      }
  }
}

##11. Create the DB Schema in a MYSQL DB

Connect to the MySQL DB which is to be used for this application and create a new DB Schema named studentEnrollment using the MySQL Workbench. This is necessary as the DB Schema name of studentEnrollment is specified in the dataSource bean in springConfig.xml

Once the studentEnrollment DB Schema is created, create a table named student inside the DB Schema using the CREATE TABLE statement as follows:

CREATE TABLE `student` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `dateOfBirth` datetime NOT NULL,
  `emailAddress` varchar(255) NOT NULL,
  `firstName` varchar(255) NOT NULL,
  `lastName` varchar(255) NOT NULL,
  `password` varchar(8) NOT NULL,
  `userName` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=latin1;

##12. Deploying the Application on Tomcat Server

Once the above steps are complete and the project is successfully built, the Java web application is ready to deployed on the Tomcat Server 7.

The Java web application can be deployed locally by right clicking on the project and choosing the “Run As->Run on Server” option.

The same can be deployed remotely on any native server that supports Tomcat by copying the WAR file (Right click on the project and choose Export as WAR File option) to /var/lib/tomcat7 folder (or appropriate tomcat directory) and restarting the tomcat server.

##13. Clone or Download code

If using git, clone a copy of this project here: https://github.com/elizabetht/StudentEnrollmentWithMyBatis.git

In case of not using git, download the project as ZIP or tar.gz file here: https://github.com/elizabetht/StudentEnrollmentWithMyBatis/releases/tag/1.7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SYPRO示例项目源码和EasyUI入门视频教程,视频在我的百度网盘中,可以下载学习: sypro示例程序(springMvc+hibernate4+easyui) sshe示例程序(struts2+spring3+hibernate4+easyui)(Maven构建) easyui1.2.6整站文件.zip jquery1.7.2中文API修正版.chm jquery.easyui-1.2.5源码未压缩版.rar jquery-easyui-1.2.6.zip JQuery-esqyUI中文-1.2.5API.CHM 第01课(大概介绍一下easyui,和组织的地址,官方网站等) 第02课(介绍easyloader组件和easyui怎样使用) 第03课(parser组件panel组件) 第04课(通过用户登录来演示dialog、ajax的使用,serialize方法的使用,前后台怎样交互等) 第05课(讲解easyui的form控件、validatebox控件、怎样跟后台交互) 第06课(讲解easyui的layout的使用) 第07课(讲解datagrid的基本应用,后台交互,排序功能) 第08课(讲解datagrid的查询,toolbar的多种创建方式,清空查询条件,扩展一个form序列化object的方法,load、reload方法的使用和区别,datetimebox初始化时需要注意的问题) 第09课(讲解datagrid的行编辑模式,增加、删除、修改,扩展editor的类型,扩展datagrid,增加动态改变editor属性,简单介绍了form的load,简单介绍了弹窗编辑模式,讲解了双击行开启编辑模式,选择行开启编辑模式,取消编辑模式。讲解insertRow、appendRow、getRowIndex、getSelections、unselectAll、rejectChanges等方法的使用和区别) 第10课(datagrid增加、删除、修改功能,结合后台讲解,怎样获得增加或删除的数据,怎样传递到后台,如果添加或修改不成功,怎样回滚操作,后台操作成功,保持状态等。和清空datagrid的简单方法。) 第11课(介绍一些easyui群,介绍easyui论坛等信息。datagrid冻结列讲解,datagrid右键菜单讲解,forzenColumns与fitColumns的应用) 第12课(讲解datagrid的formatter的应用,格式化时间、行样式、列样式、表头居中,内容居右等应用)(formatter的提示功能,由于录制问题,没有录制到提示信息,所以大家自己去测试一下吧,不能重新录制了。) 第13课(讲解easyui更换主题皮肤) 第14课(讲解tree的应用,tree的初始化方式、异步tree都需要那些配置、tree的lines、url、checkbox、属性,onLoadSuccess事件,getChecked方法的应用) 第15课(继续讲解tree的使用,介绍tree需要的JSON格式、常用事件和方法,结合tabs的使用等) 第16课(tree数据载入后自动选中想要选择的节点、tree的iconCls需要注意的地方、js中不可以用search当function名称、combo组件的基本使用) 第17课(combobox属性介绍、combobox的autocomplete功能、联动功能,本地过滤和远程过滤功能、combobox方法介绍。简要描述datagrid行编辑模式中editor类型是combobox需要注意的问题。简要描述easyui1.3的data-options初始化方式。) 第18课(讲解combotree组件的使用) 第19课(讲解combogrid组件和treegrid组件的使用) 第20课(讲解PropertyGrid组件的使用)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值