简介:ssm把spring springmvc mybatis三个框架整合再一起。 spring和mybatis框架。把mybatis配置文件的内容整合到spring配置文件中
目录
1.项目布局
2.在pom文件引入相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>qy165spring_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--spring webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--mysql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--spring和mybatis整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--其他依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>
3.数据库sql
部门表tbl_dept
CREATE TABLE `tbl_dept` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`loc` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_dept
-- ----------------------------
INSERT INTO `tbl_dept` VALUES ('1', '研发部', '郑州');
INSERT INTO `tbl_dept` VALUES ('2', '市场部', '北京');
INSERT INTO `tbl_dept` VALUES ('3', '财务部', '上海');
INSERT INTO `tbl_dept` VALUES ('4', '人事部', '杭州');
员工表tbl_emp
DROP TABLE IF EXISTS `tbl_emp`;
CREATE TABLE `tbl_emp` (
`id` int(0) NOT NULL AUTO_INCREMENT ,
`name` varchar(255) ,
`salary` decimal(10, 2) ,
`birthday` date ,
`dept_id` int(0) ,
PRIMARY KEY (`id`) USING BTREE
)
INSERT INTO `tbl_emp` VALUES (1, '刘德华', 2500.00, '2000-10-11', 1);
INSERT INTO `tbl_emp` VALUES (2, '张学友', 3200.00, '2001-11-15', 2);
INSERT INTO `tbl_emp` VALUES (3, '黎明', 2800.00, '2002-12-12', 4);
INSERT INTO `tbl_emp` VALUES (4, '郭富城', 5000.00, '2008-11-12', 3);
INSERT INTO `tbl_emp` VALUES (5, '闫克起', 50000.00, '2010-12-12', 1);
4.创建springmvc配置文件
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.包扫描-->
<context:component-scan base-package="com.wqg"/>
<!--2.注解驱动-->
<mvc:annotation-driven/>
<!--3.静态资源放行-->
<mvc:default-servlet-handler/>
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--5.配置数据源-->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/qy165ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--6. SqlSessionFactory:理解为mybatis文件
spring整合mybatis:把mybatis配置文件的内容整合过来
封装一个类SqlSessionFactoryBean,整合mybatis配置文件的内容
-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--指定mybatis映射文件的路径-->
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
</bean>
<!--7.设置dao接口的代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!--dao接口所在的包-->
<property name="basePackage" value="com.wqg.dao"/>
</bean>
</beans>
5.编辑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">
<!--注册DispatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--如果没有指定参数默认解析WEB-INF/servlerName-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.pojo实体类
@Data
public class Dept {
private Integer id;
private String name;
private String loc;
}
@Data
public class Emp {
private Integer id;
private String name;
//小数类型 不要使用double或float 因为它有精度丢失问题
private BigDecimal salary;
private Date birthday;
private Integer deptId;
}
7.dao和mapper映射文件
创建EmpDao.java
public interface EmpDao {
//查询所有
public List<Emp>findAll();
}
创建EmpMapper.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">
<!--namesapce的内容必须和对应的接口路径映射-->
<mapper namespace="com.wqg.dao.EmpDao">
<select id="findAll" resultType="com.wqg.pojo.Emp">
select * from tbl_emp
</select>
</mapper>
8.创建service包
创建接口EmpService
public interface EmpService {
//返回所有员工
public List<Emp> selectAll();
}
创建实现类EmpServiceImpl
@Service //spring扫描到该注解时,会创建该类对象,并注入相关的依赖属性
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDao empDao;
@Override
public List<Emp> selectAll() {
return empDao.findAll();
}
}
9.创建controller层
@Controller //有spring创建该类对象,并把创建的对象交于spring管理
public class EmpController {
@Autowired //按照类型匹配
private EmpService empService; //EmpService类对象必须再spring中创建好
@RequestMapping("/list")
public String list(HttpServletRequest request){
List<Emp> emps = empService.selectAll();
request.setAttribute("emps",emps);
return "list";
}
}
10.创建list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${emps}
</body>
</html>
11.运行Tomcat查询输入list
结束
ssm增删改查代更...