文章目录
一、开发环境准备
- JDK1.8
- MySQL8
- idea2021
- Tomcat8.5.87
- Apache-Maven3.9
二、搭建SSM
2.1新建Maven项目
【File】-> 【new Project】-> 【选择Archetype】
2.2项目整体结构
项目的整体结构: 包括
resources
目录下的xml和
webapp
目录下文件。如果不包含某些文件,就自己创建。
MVC结构、spring-config.xml(applicationContext.xml)、jdbc.properties(数据源)、mybatis-config.xml(mybatis配置类)、spring-mvc.xml(springMVC前端控制器配置类)、web.xml(web项目配置类)
2.3spring-config.xml配置
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"><!--引入配置类--><context:property-placeholderlocation="classpath:jdbc.properties"/><!--声明数据源DataSource--><beanid="myDataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close"><propertyname="url"value="${database.url}"/><propertyname="username"value="${database.username}"/><propertyname="password"value="${database.password}"/></bean><!--声明SqlSessionFactoryBean,在这个类的内部,创建SqlSessionFactory--><beanid="factory"class="org.mybatis.spring.SqlSessionFactoryBean"><!--指定数据源--><propertyname="dataSource"ref="myDataSource"/><!--指定mybatis主配置文件
resource可以直接使用value赋值
--><propertyname="configLocation"value="classpath:mybatis-config.xml"/></bean><!-- 使用MapperScannerConfigurer自定生成代理实现类
MapperScan
nerConfigurer是spring和mybatis整合的mybatis-spring的jar包中提供的一个类。
--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--指定SqlSessionFactory的名称--><propertyname="sqlSessionFactoryBeanName"value="factory"/><!-- 给指定包下的接口生成代理实现类 --><propertyname="basePackage"value="com.shenxm.mapper"/></bean></beans>
2.4jdbc.properties配置
database.driver=com.mysql.cj.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/demo
database.username=root
database.password=123456
2.5mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--引入jdbc.properties配置文件--><propertiesresource="jdbc.properties"/><settings><!--配置运行的sql在控制台打印输出--><settingname="logImpl"value="STDOUT_LOGGING"/><!--设置映射方式 autoMappingBehavior属性
NONE:不自动进行映射
PARTIAL:默认值,对没有嵌套结果集自动进行映射
FILL:对所有结果集进行自动映射,一般情况下,如果有结果集我们也使用ReaultMap,所以FULL很少用
--><settingname="autoMappingBehavior"value="PARTIAL"/><!--驼峰映射
开启驼峰映射后:例如数据库字段中的nick_name,在PO中属性却是nickName也可以自动匹配上(mybatis会自动将下划线更改成驼峰类型)
--><settingname="mapUnderscoreToCamelCase"value="true"/><!--开启延迟加载的开关 默认是false --><settingname="lazyLoadingEnabled"value="true"/><!-- <!–发生延迟加载的时候, 是否完全加载 默认是 false (3.1.4版本之后) 一般不配置 用默认的就可以–>
<setting name="aggressiveLazyLoading" value="true"/>--></settings><!--注入映射器--><mappers><!--映射器(xml)所在包--><packagename="com.shenxm.mapper"/></mappers></configuration>
2.6spring-mvc.xml配置
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"><!-- 自动注册组件 --><mvc:annotation-driven/><!--指定组件扫描路径--><context:component-scanbase-package="com.shenxm"/></beans>
2.7web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-appxmlns="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"><!--配置全局过滤filter--><filter><filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--配置springMVC的前端控制器--><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><context-param><!-- contextConfigLocation:名称是固定的,表示自定义的spring配置文件的路径 --><param-name>contextConfigLocation</param-name><!-- 自定义配置文件的路径 --><param-value>classpath:spring-config.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
2.8index.jsp
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" language="java" isELIgnored="false" %>
<html>
<body>
<h2>测试</h2>
<p>创建学生用户信息</p>
<form action="${pageContext.request.contextPath}/test/add" method="post">
姓名:<input type="text" name="name"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="注册学生">
</form>
</body>
</html>
2.9ok.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
<h2>添加成功,这是一个跳转页面!!!</h2>
</head>
<body>
</body>
</html>
2.10Java代码
controller层:
packagecom.shenxm.controller;importcom.shenxm.po.Test;importcom.shenxm.service.TestService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/test")publicclassDemoController{@AutowiredprivateTestService testService;@RequestMapping("/add")publicModelAndViewaddOneTest(Test test){ModelAndView modelAndView =newModelAndView();
testService.addTest(test);System.out.println("addTest方法执行完成");
modelAndView.setViewName("/ok.jsp");return modelAndView;}}
service层接口类:
packagecom.shenxm.service;importcom.shenxm.po.Test;publicinterfaceTestService{intaddTest(Test test);}
service层实现类:
packagecom.shenxm.service.impl;importcom.shenxm.mapper.TestMapper;importcom.shenxm.po.Test;importcom.shenxm.service.TestService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;@ServicepublicclassTestServiceImplimplementsTestService{@AutowiredprivateTestMapper testMapper;@OverridepublicintaddTest(Test test){int row = testMapper.addTest(test);return row;}}
mapper层:
TestMapper.java
packagecom.shenxm.mapper;importcom.shenxm.po.Test;importorg.springframework.stereotype.Component;@ComponentpublicinterfaceTestMapper{intaddTest(Test test);}
TestMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.shenxm.mapper.TestMapper"><insertid="addTest"parameterType="com.shenxm.po.Test"useGeneratedKeys="true"keyProperty="id">
insert into test values (null,#{name},#{age})
</insert></mapper>
po包:
packagecom.shenxm.po;importlombok.AllArgsConstructor;importlombok.Builder;importlombok.Data;importlombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructor@BuilderpublicclassTest{/** 自增的id */privateInteger id;/** 姓名 */privateString name;/** 年龄 */privateInteger age;}
2.11测试
tomcat8.5配置启动
访问页面:(成功)
版权归原作者 1024de小shen 所有, 如有侵权,请联系我们删除。