spring springmvc mybatis整合实例代码
时间: 2023-07-27 11:33:41 浏览: 117
springMVC mybatis整合实例
4星 · 用户满意度95%
以下是一个简单的 Spring + Spring MVC + MyBatis 整合的实例代码:
1. 创建 Maven 项目,并添加以下依赖:
```xml
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- MyBatis-Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
```
2. 在 `src/main/resources` 目录下创建 `applicationContext.xml` 文件,并添加以下内容:
```xml
<!-- 数据库配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:mapper/*.xml" />
</bean>
<!-- Mapper 扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao" />
</bean>
<!-- Service 扫描 -->
<context:component-scan base-package="com.example.service" />
<!-- Controller 扫描 -->
<context:component-scan base-package="com.example.controller" />
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
```
3. 创建一个 DAO 接口,例如:
```java
public interface UserMapper {
User getUserById(int id);
void insertUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
```
4. 在 `src/main/resources` 目录下创建 `mapper/UserMapper.xml` 文件,并添加以下内容:
```xml
<mapper namespace="com.example.dao.UserMapper">
<select id="getUserById" resultType="com.example.model.User">
select * from user where id = #{id}
</select>
<insert id="insertUser">
insert into user(name, age) values(#{name}, #{age})
</insert>
<update id="updateUser">
update user set name = #{name}, age = #{age} where id = #{id}
</update>
<delete id="deleteUser">
delete from user where id = #{id}
</delete>
</mapper>
```
5. 创建一个 Service 接口,例如:
```java
public interface UserService {
User getUserById(int id);
void insertUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
```
6. 实现 Service 接口,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(int id) {
return userMapper.getUserById(id);
}
@Override
public void insertUser(User user) {
userMapper.insertUser(user);
}
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
@Override
public void deleteUser(int id) {
userMapper.deleteUser(id);
}
}
```
7. 创建一个 Controller 类,例如:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/{id}")
public String getUserById(@PathVariable int id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String insertUser(@ModelAttribute User user) {
userService.insertUser(user);
return "redirect:/";
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
public String updateUser(@PathVariable int id, @ModelAttribute User user) {
user.setId(id);
userService.updateUser(user);
return "redirect:/";
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable int id) {
userService.deleteUser(id);
return "redirect:/";
}
}
```
8. 在 `src/main/webapp/WEB-INF/views` 目录下创建 `user.jsp` 文件,并添加以下内容:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>User Info</title>
</head>
<body>
<h1>User Info</h1>
<table>
<tr>
<td>ID:</td>
<td>${user.id}</td>
</tr>
<tr>
<td>Name:</td>
<td>${user.name}</td>
</tr>
<tr>
<td>Age:</td>
<td>${user.age}</td>
</tr>
</table>
<hr>
<h2>Add User</h2>
<form action="${pageContext.request.contextPath}/user" method="POST">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Add"></td>
</tr>
</table>
</form>
<hr>
<h2>Update User</h2>
<form action="${pageContext.request.contextPath}/user/${user.id}" method="POST">
<input type="hidden" name="_method" value="PUT">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="${user.name}"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age" value="${user.age}"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Update"></td>
</tr>
</table>
</form>
<hr>
<h2>Delete User</h2>
<form action="${pageContext.request.contextPath}/user/${user.id}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="Delete">
</form>
</body>
</html>
```
9. 运行项目,访问 `http://localhost:8080/user/{id}` 即可查看用户信息,访问 `http://localhost:8080/` 可以添加、修改、删除用户信息。
阅读全文