SSM
环境搭建
- IDEA
- maven
- tomcat
- mysql
创建数据库,创建表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名称',
`birthday` date NULL DEFAULT NULL COMMENT '生日',
`sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别',
`address` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 27 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '王五', NULL, '2', NULL);
INSERT INTO `user` VALUES (10, '张三', '2014-07-10', '1', '北京市');
INSERT INTO `user` VALUES (16, '张小明', NULL, '1', '河南郑州');
INSERT INTO `user` VALUES (22, '陈小明', NULL, '1', '河南郑州');
INSERT INTO `user` VALUES (24, '张三丰', NULL, '1', '河南郑州');
INSERT INTO `user` VALUES (25, '陈小明', NULL, '1', '河南郑州');
INSERT INTO `user` VALUES (26, '王五', NULL, NULL, NULL);
SET FOREIGN_KEY_CHECKS = 1;
新建Maven项目,导入pom依赖
需要的依赖
单元测试(junit), 数据库 ,连接池 jsp, servlet ,mybatis, mybatis-spring,springmvc ,spring-jdbc
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.13</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
Maven资源过滤设置
参考狂神的
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
配置database.properties数据源
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/2023java?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
jdbc.username = root
jdbc.password = 123456
配置spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载database.properties配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--创建sqlsessionfactor-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--加载映射文件-->
<property name="mapperLocations" value="classpath:com/wei/mapper/**/*.xml"/>
</bean>
<!--扫描mapper层接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wei.mapper"/>
</bean>
</beans>
配置spring-service
<?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"
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">
<!--开启注解,扫描 添加spring注解的包-->
<context:component-scan base-package="com.wei.service"/>
<!--配置事务管理器-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--加载数据库连接-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
配置spring-mvc
<?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
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启mvc注解 -->
<mvc:annotation-driven/>
<!--扫描添加了spring注解的包-->
<context:component-scan base-package="com.wei.controller"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--配置后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置视图解析器-->
<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:applicationContext.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>
<!--设置编码-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--设置session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
配置applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--导入dao层-->
<import resource="classpath:spring-dao.xml"/>
<!--导入service层-->
<import resource="classpath:spring-service.xml"/>
<!--导入controller层-->
<import resource="classpath:spring-mvc.xml"/>
</beans>
创建UserMapper.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.wei.mapper.UserMapper">
<select id="findUserById" resultType="com.wei.pojo.User">
select * from user where id= #{id}
</select>
<select id="findUserList" resultType="com.wei.pojo.User">
select * from user
</select>
<delete id="deteltUser" >
delete from user where id = #{id}
</delete>
<update id="updateUser" >
update user set username = #{username} ,birthday=#{birthday} ,sex=#{sex} ,address=#{address} where id = #{id}
</update>
<insert id="addUser" >
insert into user (username, birthday, sex, address) values (#{username} ,#{birthday} ,#{sex} ,#{address});
</insert>
</mapper>
创建pojo实体类
package com.wei.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
创建dao层的UserMapper
package com.wei.mapper;
import com.wei.pojo.User;
import java.util.List;
public interface UserMapper {
int addUser(User user);
int deteltUser(Integer id);
int updateUser(Integer id);
User findUserById(Integer id);
List<User> findUserList();
}
创建service层的UserService
package com.wei.service;
import com.wei.pojo.User;
import java.util.List;
public interface UserService {
int addUser(User user);
int deteltUser(Integer id);
int updateUser(Integer id);
User findUserById(Integer id);
List<User> findUserList();
}
创建impl
package com.wei.service.impl;
import com.wei.mapper.UserMapper;
import com.wei.pojo.User;
import com.wei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int deteltUser(Integer id) {
return userMapper.deteltUser(id);
}
@Override
public int updateUser(Integer id) {
return userMapper.updateUser(id);
}
@Override
public User findUserById(Integer id) {
return userMapper.findUserById(id);
}
@Override
public List<User> findUserList() {
return userMapper.findUserList();
}
}
创建controller控制器(只写了两个接口用来测试,没有写全部的)
package com.wei.controller;
import com.wei.pojo.User;
import com.wei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public String allUser(Model model) {
List<User> userList = userService.findUserList();
model.addAttribute("user", userList);
return "user";
}
@RequestMapping("/findone")
public String oneUser(Model model) {
int id = (int) model.getAttribute("id");
User oneuser = userService.findUserById(id);
model.addAttribute("oneuser", oneuser);
return "user";
}
}
测试
import com.wei.pojo.User;
import com.wei.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class test {
@Test
public void tes(){
ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService serviceImpl = (UserService) ApplicationContext.getBean("userServiceImpl");
List<User> userList = serviceImpl.findUserList();
for (User user : userList) {
System.out.println(user);
}
}
}
结果
User{id=1, username='王五', birthday=null, sex='2', address='null'}
User{id=10, username='张三', birthday=Thu Jul 10 00:00:00 CST 2014, sex='1', address='北京市'}
User{id=16, username='张小明', birthday=null, sex='1', address='河南郑州'}
User{id=22, username='陈小明', birthday=null, sex='1', address='河南郑州'}
User{id=24, username='张三丰', birthday=null, sex='1', address='河南郑州'}
User{id=25, username='陈小明', birthday=null, sex='1', address='河南郑州'}
User{id=26, username='王五', birthday=null, sex='null', address='null'}
配置前端页面
在WEB-INF目录下创建一个jsp目录
在jsp目录下创建一个user 的jsp
可能会出现的问题
1、在配置文件中,xml头文件导入不正确,或者版本不对,pom依赖版本冲突都会导致项目无法启动
2、在启动过程中提示找不到类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o8ZXY3NO-1679556846699)(E:\Desktop\错误.png)]
00 CST 2014, sex=‘1’, address=‘北京市’}
User{id=16, username=‘张小明’, birthday=null, sex=‘1’, address=‘河南郑州’}
User{id=22, username=‘陈小明’, birthday=null, sex=‘1’, address=‘河南郑州’}
User{id=24, username=‘张三丰’, birthday=null, sex=‘1’, address=‘河南郑州’}
User{id=25, username=‘陈小明’, birthday=null, sex=‘1’, address=‘河南郑州’}
User{id=26, username=‘王五’, birthday=null, sex=‘null’, address=‘null’}
配置前端页面
> 在WEB-INF目录下创建一个jsp目录
>
> 在jsp目录下创建一个user 的jsp
## 可能会出现的问题
> 1、在配置文件中,xml头文件导入不正确,或者版本不对,pom依赖版本冲突都会导致项目无法启动
>
> 2、在启动过程中提示找不到类
>
> [外链图片转存中...(img-o8ZXY3NO-1679556846699)]
jsp页面没有实现