1、创建用户表,插入测试数据 2、创建web工程,导入所有依赖 3、创建项目包结构,dao、service、controller、entity 4、创建用户实体类 5、创建用户Dao接口,在接口中声明查询所有用户的方法 6、创建用户Service接口和实现类 7、创建用户Contorller类 8、编写项目的配置文件,UserMapper.xml、applicationContext.xml、spring-mvc.xml、web.xml 9、部署项目,并使用postman测试查询所有用户的方法
时间: 2023-07-10 17:08:25 浏览: 187
好的,以下是具体的步骤:
1. 创建用户表,插入测试数据
首先,需要在数据库中创建一个用户表,例如:
```sql
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
email varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
然后,插入一些测试数据:
```sql
INSERT INTO user (username, password, email) VALUES ('user1', 'password1', 'user1@example.com');
INSERT INTO user (username, password, email) VALUES ('user2', 'password2', 'user2@example.com');
INSERT INTO user (username, password, email) VALUES ('user3', 'password3', 'user3@example.com');
```
2. 创建web工程,导入所有依赖
在IDE中创建一个新的web项目,然后在项目的pom.xml文件中添加所需的依赖,例如:
```xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- 其他依赖 -->
</dependencies>
```
3. 创建项目包结构,dao、service、controller、entity
在src/main/java目录下创建以下包结构:
- dao:存放数据访问层的接口和XML文件
- service:存放服务层的接口和实现类
- controller:存放控制器类
- entity:存放实体类
4. 创建用户实体类
在entity包下创建User实体类,例如:
```java
public class User {
private Integer id;
private String username;
private String password;
private String email;
// getter和setter方法
}
```
5. 创建用户Dao接口,在接口中声明查询所有用户的方法
在dao包下创建UserDao接口,例如:
```java
public interface UserDao {
List<User> findAll();
}
```
然后,在dao包下创建UserMapper.xml文件,实现findAll方法的SQL语句,例如:
```xml
<mapper namespace="com.example.dao.UserDao">
<select id="findAll" resultType="com.example.entity.User">
SELECT * FROM user
</select>
</mapper>
```
6. 创建用户Service接口和实现类
在service包下创建UserService接口和UserServiceImpl实现类,例如:
```java
public interface UserService {
List<User> findAll();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
```
7. 创建用户Contorller类
在controller包下创建UserController类,例如:
```java
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
public List<User> findAll() {
return userService.findAll();
}
}
```
8. 编写项目的配置文件,UserMapper.xml、applicationContext.xml、spring-mvc.xml、web.xml
在src/main/resources目录下创建以下配置文件:
- UserMapper.xml:数据访问层的映射文件
- applicationContext.xml:Spring的配置文件
- spring-mvc.xml:Spring MVC的配置文件
- web.xml:Web应用的部署描述文件
具体的配置可以参考以下示例:
UserMapper.xml:
```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="com.example.dao.UserDao">
<select id="findAll" resultType="com.example.entity.User">
SELECT * FROM user
</select>
</mapper>
```
applicationContext.xml:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 数据源配置 -->
<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="123456" />
</bean>
<!-- MyBatis的SqlSessionFactory配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:com/example/dao/*Mapper.xml" />
</bean>
<!-- MyBatis的MapperScannerConfigurer配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao" />
</bean>
<!-- Service和Dao的自动扫描 -->
<context:component-scan base-package="com.example.service com.example.dao" />
</beans>
```
spring-mvc.xml:
```xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描控制器 -->
<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>
<!-- 配置静态资源访问 -->
<mvc:resources mapping="/static/**" location="/static/" />
<!-- 配置DispatcherServlet -->
<mvc:annotation-driven />
</beans>
```
web.xml:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<display-name>SpringMVCExample</display-name>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
```
9. 部署项目,并使用postman测试查询所有用户的方法
将项目部署到Tomcat服务器上,启动Tomcat服务器,然后使用postman等工具测试查询所有用户的方法,例如:
- URL:http://localhost:8080/users
- Method:GET
- Header:Content-Type: application/json
- Response:
```json
[
{
"id": 1,
"username": "user1",
"password": "password1",
"email": "user1@example.com"
},
{
"id": 2,
"username": "user2",
"password": "password2",
"email": "user2@example.com"
},
{
"id": 3,
"username": "user3",
"password": "password3",
"email": "user3@example.com"
}
]
```
以上就是一个简单的Spring MVC + MyBatis项目的搭建过程。
阅读全文