我的简书连接:http://www.jianshu.com/p/1730df498855
这里使用的是xml配置mybatis,如喜欢注解,请待下一篇。
在pom.xml中添加mybatis-spring、mybatis、mysql-connector-java(用于mysql连接)、jackson-core、jackson-databind(用于返回对象直接映射成json),整体工程现在配置如下:
<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.0modelVersion>
<groupId>com.nothinggroupId>
<artifactId>hellospringmvcartifactId>
<packaging>warpackaging>
<version>1.0-SNAPSHOTversion>
<name>hellospringmvc Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.7.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.7.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-jpaartifactId>
<version>1.9.4.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20160212version>
dependency>
dependencies>
<build>
<finalName>hellospringmvcfinalName>
build>
project>
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mysql" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="userMapper.xml" />
mappers>
configuration>
需要注意的是,如下与jdbc连接的代码差不多的,这里使用xml配置,由于我使用的是mysql(如使用其他数据库请百度),url中为数据库的名称,我这里是mysql。后面两项是username 和password需自行配置。
这里需要注意路径:本文放在src/main/resources 目录下。我在这卡了很久。
<mapper namespace="userMapper">
<select id="findUserById" parameterType="int" resultType="com.nothing.Model.User">
select * from user_nothing where id = #{id}
select>
mapper>
这里使用xml配置sql语句。user_nothing 在mysql数据库中创建的表。三个字段,待会给出。parameterType为输入参数 id的类型int,esultType="com.nothing.Model.User"为返回值User对象。
package com.nothing.Model;
public class User {
private String username;
private String password;
private int id;
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;
}
public User(String username, String password, int id) {
this.username = username;
this.password = password;
this.id = id;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User() {
}
}
public interface IUserDao {
public User findUserById(int id);
}
UserDaoImpl 才是正真的实现类,参与数据库curd,本文只有简单的单个查询。
public class UserDaoImpl implements IUserDao{
private SqlSessionFactory sessionFactory;
private SqlSession session;
public UserDaoImpl() {
String resource = "conf.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sessionFactory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
public User findUserById(int id) {
String statement = "userMapper.findUserById";
User user = (User)session.selectOne(statement, 1);
return user;
}
}
public interface IUserService {
public User findUserById(int id);
}
UserServiceImpl真正实现
public class UserServiceImpl implements IUserService{
private IUserDao userDao;
public UserServiceImpl() {
userDao = new UserDaoImpl();
}
public User findUserById(int id) {
return userDao.findUserById(id);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<h2>Hello World!h2>
<sf:form method="post" modelAttribute="user" action="/toJson">
用户名:<sf:input path="username"/>
密码:<sf:password path="password"/>
<input type="submit" value="提交">
sf:form>
body>
html>
这里使用jstl标签库。
@Controller
public class MainController {
private IUserService service = new UserServiceImpl();
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index( User user) {
return "index";
}
@RequestMapping(value ="/toJson",method=RequestMethod.POST)
@ResponseBody
public User toJson(User user){
return service.findUserById(2);
}
}