
1、创建项目,然后配置maven

配置好maven
引入文件夹


2、引入依赖
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.8version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.3.8version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>5.3.8version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.7version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.6version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.2.6version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.49version>
- dependency>
- <dependency>
- <groupId>maven_repository.javax.servletgroupId>
- <artifactId>servlet-apiartifactId>
- <version>2.5version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.58version>
- dependency>
- dependencies>

3、配置tomcat




准备好这些包、方便等下测试
com.xg.controller
com.xg.service
com.xg.dao
com.xg.pojo

4、配置springmvc和spring

配置web.xml
- <web-app>
- <display-name>Archetype Created Web Applicationdisplay-name>
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext.xmlparam-value>
- context-param>
-
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
- <servlet>
- <servlet-name>dispatcherservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>dispatcherservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
- <filter>
- <filter-name>CharacterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>utf-8param-value>
- init-param>
- <init-param>
- <param-name>forceRequestEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
- <filter>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>

创建dispatcher-servlet.xml

配置 dispatcher-servlet.xml
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
-
- <context:component-scan base-package="com.xg" use-default-filters="false">
-
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
-
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/">property>
- <property name="suffix" value=".html">property>
- bean>
-
-
-
- <mvc:default-servlet-handler/>
-
- <mvc:annotation-driven>
- <mvc:message-converters register-defaults="true">
-
- <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>application/jsonvalue>
- <value>text/html;charset=UTF-8value>
- list>
- property>
-
- bean>
- mvc:message-converters>
- mvc:annotation-driven>
-
- beans>
-
创建
applicationContext.xml

创建包views
在包下创建success.html
5、测试
创建TestController

- @Controller
- public class TestController {
- @RequestMapping(value = "/hi")
- public String hi() {
- System.out.println("TestController hi()");
- return "success";
- }
- }

整合mybatis
创建jdbc.properties

- jdbc.userName=root
- jdbc.password=123456
- jdbc.driverClass=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/ssm?userSSL=true&userUnicode=true&characterEncoding=UTF-8
在pom.xml中加入
- <dependency>
- <groupId>maven_repository.org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.3version>
- dependency>

applicationContext.xml编辑

- "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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop
- https://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <context:component-scan base-package="com.xg">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="url" value="${jdbc.url}">property>
- <property name="driverClassName" value="${jdbc.driverClass}">property>
- <property name="username" value="${jdbc.userName}">property>
- <property name="password" value="${jdbc.password}">property>
- bean>
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="configLocation" value="classpath:mybatis-config.xml">property>
-
- <property name="dataSource" ref="pooledDataSource">property>
-
- <property name="mapperLocations" value="classpath:mapper/*.xml">property>
- bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
- <property name="basePackage" value="com.xg.dao">property>
- bean>
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
- <property name="dataSource" ref="pooledDataSource">property>
- bean>
-
-
-
- <aop:config>
-
- <aop:pointcut id="txPoint" expression="execution(* com.xg.service..*(..))"/>
-
- <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
- aop:config>
-
- <tx:advice id="txAdvice">
- <tx:attributes>
-
- <tx:method name="*"/>
-
- <tx:method name="get*" read-only="true"/>
- tx:attributes>
- tx:advice>
- beans>
创建mybatis-config.xml


mybatis-config.xml配置
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- configuration>
创建好数据库
- CREATE DATABASE ssm;
- USE ssm;
-
- CREATE TABLE person(
- id INT,
- NAME VARCHAR(32),
- age INT
- )
- INSERT INTO person VALUES(1,'张三',18);
- INSERT INTO person VALUES(2,'李四',20);
- SELECT * FROM person;
准备好

、
Person
- public class Person {
- private Integer id;
- private String name;
- private Integer age;
-
- @Override
- public String toString() {
- return "Person{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
- }
PersonDao
- @Repository
- public interface PersonDao {
-
- public List
getPersonList(); - }
PersonService
- @Service
- public class PersonService {
- @Autowired
- private PersonDao personDao;
-
- public List
getPersonList(){ - return personDao.getPersonList();
- };
-
- }
TestController
- @Controller
- public class TestController {
-
- @Autowired
- private PersonService personService;
-
- @RequestMapping(value = "/hi")
- public String hi() {
- System.out.println("TestController hi()");
- return "success";
- }
-
- @ResponseBody
- @RequestMapping(value = "/personList")
- public List
personList(HttpServletRequest request, HttpServletResponse response) { - List
personList = personService.getPersonList(); -
- return personList;
- }
- }
resource下创建mapper包
在mapper下

编辑PersonMapper.xml
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.xg.dao.PersonDao">
- <select id="getPersonList" resultType="com.xg.pojo.Person">
- select * from person
- select>
- mapper>
测试
