SpringMvc+Mybatis+Maven+Mysql做一个CRUD的简单例子

时间:2021-09-09 23:27:09

本文档结合 SpringMVC、 Mybatis、 MySQL,说明如何实现一个简单的数据库单表 CRUD操作。开发工具使用集成了spring mvc的eclipse(Spring Tool Suite,简称STS)。

基础环境说明

  • Mac OS X 10.11.5
  • jre version 1.8.0_05
  • java sdk 1.8.0_05
  • STS 3.5.1
  • maven 3.2.1

以上环境安装配置好后,就可以开始demo程序了。

MySQL建表语句 

CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=big5

  

Step 1 新建spring mvc项目

使用STS新建一个spring mvc project,建立好项目后,一个基本的spring mvc web项目已经搭建 好,我们只需要在这之上配置好相关的beans,以及编写CRUD相关的代码。

可以注意一下项目中的 web.xml、 servlet-context.xml,STS已经帮我们把跟spring 相关的基本配置项配置好。

例如:web.xml中定义了请求由spring接管,以及IOC相关的配置文件名称。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <servlet>
<servlet-name>mybatis</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mybatis</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

  

Step 2 使用maven加载所需的jar包

如果使用独立安装的maven程序,需要在eclipse中指定maven的路径:
Preferences — Maven — Installations
例如,我的路径是:/usr/local/maven/maven3.2.2
maven配置好后,就可以在pom文件中添加相关的依赖项了,需要添加的相关依赖配置如下:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.goody9807.mybatis</groupId>
<artifactId>mybatis_web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatis_web</name>
<properties>
<org.springframework-version>3.2.3.RELEASE</org.springframework-version>
<slf4j-version>1.5.11</slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${slf4j-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j-version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-version}</version>
</dependency>
</dependencies>
</project>

  

需要注意跟spring相关的jar包的版本问题

保存pom文件后会自动根据配置文件下载所需的jar包。

Step 3 主要配置示例

bean相关配置

在mybatis-servlet.xml配置文件中增加如下的bean,注意schema格式 数据源:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <context:component-scan base-package="goody9807.springdemo" />
<mvc:annotation-driven />
<!-- 允许对静态资源文件的访问 --> <mvc:default-servlet-handler /> <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean> <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
lazy-init="default" autowire="default">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver">
</beans:property>
<beans:property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></beans:property>
<beans:property name="username" value="root"></beans:property>
<beans:property name="password" value="123456"></beans:property>
</beans:bean> <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="configLocation"
value="spring-mybatis-config.xml"></beans:property>
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<beans:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<beans:property name="basePackage" value="goody9807.springdemo.da" />
</beans:bean>
</beans:beans>

  

Mybatis Sql Session配置:

由spring接管的mybatis session,configLocation配置的value要和实际的mybatis配置 文件一致。

Mybatis主配置文件

spring-mybatis-config.xml配置文件示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="useGeneratedKeys" value="false" />
<setting name="autoMappingBehavior" value="PARTIAL" />
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25000" />
<setting name="safeRowBoundsEnabled" value="false" />
<setting name="mapUnderscoreToCamelCase" value="false" />
<setting name="localCacheScope" value="SESSION" />
<setting name="jdbcTypeForNull" value="OTHER" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
<typeAliases>
<typeAlias alias="User" type="goody9807.springdemo.model.User" />
</typeAliases>
<mappers>
<mapper resource="config/mapper/user.xml" />
</mappers>
</configuration>

  

mapper文件

例如:mapper/user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="goody9807.springdemo.da.UserMapper">
<select id = "get" parameterType="int" resultType="User">
select * from user where ID=#{id};
</select>
<insert id="insert" parameterType="User">
insert into user values
(#{id}, #{name}, #{age});
</insert>
<update id="update" parameterType="User">
update user set
name=#{name},age=#{age} where id=#{id};
</update>
</mapper>

  

Step 4 主要代码

定义Model User.java

package goody9807.springdemo.model;

public class User {
private int id;
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}
}

  

定义CRUD操作接口

UserMapper.java

package goody9807.springdemo.da;

import goody9807.springdemo.model.User;

public interface UserMapper {
User get(int id); void insert(User u); void update(User u);
}

  

编写Action

在Controller中增加CRUD相关的action。需要先将数据操作的mapper类注入到controller中,增加 代码如下:

@Autowired
private UserMapper _userMapper;

  

get一个数据实体

 @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public String get(@PathVariable("id") int id, Model model) { User u = _userMapper.get(id);
model.addAttribute("user", u); return "user";
}

  

注解RequestMapping说明对应的url,以及http method,其中{id}代表URL参数,和action中的参 数id对应,同时需要在参数id前增加注解:@PathVariable(“id”)

这里使用spring内置的model传递参数到view,在页面上可以采用如下语句获取数据实体内容。

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Show</title>
</head>
<body>
<div> <p>
<span>${user.getId()}</span> </p>
<p>
<strong>Name</strong> <span>${user.getName()}</span>
</p> <p>
<strong>Age</strong>
<span>${user.getAge()}</span> </p>
</div>
</body>
</html>

  

insert一个数据实体

add的get方法

@RequestMapping(value = "/add", method = RequestMethod.GET ) public String Insert() {
return "add"; }

  

add的post方法

@RequestMapping(value = "/add", method = RequestMethod.POST ) @ResponseBody
public String Insert(User u) {
_userMapper.insert(u);
return "insert a new user"; }

  

对应的view

<form action="" method="post"> <div>
<p>
<input type="text" value="${user.getId()}" name="id" />
</p> <p>
<strong>Name</strong>
<input type="text" value="${user.getName()}" name="name" /> </p>
<p>
<strong>Age</strong>
<input type="text" value="${user.getAge()}" name="age" />
</p> </div>
<div>
<input type="submit" value="Submit" />
</div> </form>

  

其他update和delete操作和上面基本类似,至此一个简单的单表CRUD程序已经能run起来了。