• SSM整合


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

     

    配置好maven 

     引入文件夹

     

     

     2、引入依赖

    1. <properties>
    2. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    3. <maven.compiler.source>1.8maven.compiler.source>
    4. <maven.compiler.target>1.8maven.compiler.target>
    5. properties>
    6. <dependencies>
    7. <dependency>
    8. <groupId>junitgroupId>
    9. <artifactId>junitartifactId>
    10. <version>4.11version>
    11. <scope>testscope>
    12. dependency>
    13. <dependency>
    14. <groupId>org.springframeworkgroupId>
    15. <artifactId>spring-webmvcartifactId>
    16. <version>5.3.8version>
    17. dependency>
    18. <dependency>
    19. <groupId>org.springframeworkgroupId>
    20. <artifactId>spring-jdbcartifactId>
    21. <version>5.3.8version>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframeworkgroupId>
    25. <artifactId>spring-aspectsartifactId>
    26. <version>5.3.8version>
    27. dependency>
    28. <dependency>
    29. <groupId>org.mybatisgroupId>
    30. <artifactId>mybatisartifactId>
    31. <version>3.5.7version>
    32. dependency>
    33. <dependency>
    34. <groupId>org.mybatisgroupId>
    35. <artifactId>mybatis-springartifactId>
    36. <version>2.0.6version>
    37. dependency>
    38. <dependency>
    39. <groupId>com.alibabagroupId>
    40. <artifactId>druidartifactId>
    41. <version>1.2.6version>
    42. dependency>
    43. <dependency>
    44. <groupId>mysqlgroupId>
    45. <artifactId>mysql-connector-javaartifactId>
    46. <version>5.1.49version>
    47. dependency>
    48. <dependency>
    49. <groupId>maven_repository.javax.servletgroupId>
    50. <artifactId>servlet-apiartifactId>
    51. <version>2.5version>
    52. dependency>
    53. <dependency>
    54. <groupId>com.alibabagroupId>
    55. <artifactId>fastjsonartifactId>
    56. <version>1.2.58version>
    57. dependency>
    58. dependencies>

     

    3、配置tomcat 

     

     

    准备好这些包、方便等下测试

    com.xg.controller

    com.xg.service

    com.xg.dao

    com.xg.pojo

     

    4、配置springmvc和spring

     

    配置web.xml 

    1. <web-app>
    2. <display-name>Archetype Created Web Applicationdisplay-name>
    3. <context-param>
    4. <param-name>contextConfigLocationparam-name>
    5. <param-value>classpath:applicationContext.xmlparam-value>
    6. context-param>
    7. <listener>
    8. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    9. listener>
    10. <servlet>
    11. <servlet-name>dispatcherservlet-name>
    12. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    13. <load-on-startup>1load-on-startup>
    14. servlet>
    15. <servlet-mapping>
    16. <servlet-name>dispatcherservlet-name>
    17. <url-pattern>/url-pattern>
    18. servlet-mapping>
    19. <filter>
    20. <filter-name>CharacterEncodingFilterfilter-name>
    21. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    22. <init-param>
    23. <param-name>encodingparam-name>
    24. <param-value>utf-8param-value>
    25. init-param>
    26. <init-param>
    27. <param-name>forceRequestEncodingparam-name>
    28. <param-value>trueparam-value>
    29. init-param>
    30. <init-param>
    31. <param-name>forceResponseEncodingparam-name>
    32. <param-value>trueparam-value>
    33. init-param>
    34. filter>
    35. <filter-mapping>
    36. <filter-name>CharacterEncodingFilterfilter-name>
    37. <url-pattern>/*url-pattern>
    38. filter-mapping>
    39. <filter>
    40. <filter-name>HiddenHttpMethodFilterfilter-name>
    41. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    42. filter>
    43. <filter-mapping>
    44. <filter-name>HiddenHttpMethodFilterfilter-name>
    45. <url-pattern>/*url-pattern>
    46. filter-mapping>
    47. web-app>

     

    创建dispatcher-servlet.xml

    配置 dispatcher-servlet.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. 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">
    6. <context:component-scan base-package="com.xg" use-default-filters="false">
    7. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    8. context:component-scan>
    9. <bean
    10. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    11. <property name="prefix" value="/WEB-INF/views/">property>
    12. <property name="suffix" value=".html">property>
    13. bean>
    14. <mvc:default-servlet-handler/>
    15. <mvc:annotation-driven>
    16. <mvc:message-converters register-defaults="true">
    17. <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
    18. <property name="supportedMediaTypes">
    19. <list>
    20. <value>application/jsonvalue>
    21. <value>text/html;charset=UTF-8value>
    22. list>
    23. property>
    24. bean>
    25. mvc:message-converters>
    26. mvc:annotation-driven>
    27. beans>

     创建

    applicationContext.xml

     创建包views

    在包下创建success.html

    5、测试

    创建TestController 

    1. @Controller
    2. public class TestController {
    3. @RequestMapping(value = "/hi")
    4. public String hi() {
    5. System.out.println("TestController hi()");
    6. return "success";
    7. }
    8. }

    整合mybatis 

    创建jdbc.properties

    1. jdbc.userName=root
    2. jdbc.password=123456
    3. jdbc.driverClass=com.mysql.jdbc.Driver
    4. jdbc.url=jdbc:mysql://localhost:3306/ssm?userSSL=true&userUnicode=true&characterEncoding=UTF-8

    在pom.xml中加入

    1. <dependency>
    2. <groupId>maven_repository.org.mybatisgroupId>
    3. <artifactId>mybatis-springartifactId>
    4. <version>2.0.3version>
    5. dependency>

    applicationContext.xml编辑

    1. "1.0" encoding="UTF-8"?>
    2. <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
    3. http://www.springframework.org/schema/beans/spring-beans.xsd
    4. http://www.springframework.org/schema/context
    5. https://www.springframework.org/schema/context/spring-context.xsd
    6. http://www.springframework.org/schema/tx
    7. http://www.springframework.org/schema/tx/spring-tx.xsd
    8. http://www.springframework.org/schema/aop
    9. https://www.springframework.org/schema/aop/spring-aop.xsd">
    10. <context:component-scan base-package="com.xg">
    11. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    12. context:component-scan>
    13. <context:property-placeholder location="classpath:jdbc.properties"/>
    14. <bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource">
    15. <property name="url" value="${jdbc.url}">property>
    16. <property name="driverClassName" value="${jdbc.driverClass}">property>
    17. <property name="username" value="${jdbc.userName}">property>
    18. <property name="password" value="${jdbc.password}">property>
    19. bean>
    20. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    21. <property name="configLocation" value="classpath:mybatis-config.xml">property>
    22. <property name="dataSource" ref="pooledDataSource">property>
    23. <property name="mapperLocations" value="classpath:mapper/*.xml">property>
    24. bean>
    25. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    26. <property name="basePackage" value="com.xg.dao">property>
    27. bean>
    28. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    29. <property name="dataSource" ref="pooledDataSource">property>
    30. bean>
    31. <aop:config>
    32. <aop:pointcut id="txPoint" expression="execution(* com.xg.service..*(..))"/>
    33. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    34. aop:config>
    35. <tx:advice id="txAdvice">
    36. <tx:attributes>
    37. <tx:method name="*"/>
    38. <tx:method name="get*" read-only="true"/>
    39. tx:attributes>
    40. tx:advice>
    41. beans>

    创建mybatis-config.xml 

    mybatis-config.xml配置

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. configuration>

    创建好数据库 

    1. CREATE DATABASE ssm;
    2. USE ssm;
    3. CREATE TABLE person(
    4. id INT,
    5. NAME VARCHAR(32),
    6. age INT
    7. )
    8. INSERT INTO person VALUES(1,'张三',18);
    9. INSERT INTO person VALUES(2,'李四',20);
    10. SELECT * FROM person;

     准备好

    Person

    1. public class Person {
    2. private Integer id;
    3. private String name;
    4. private Integer age;
    5. @Override
    6. public String toString() {
    7. return "Person{" +
    8. "id=" + id +
    9. ", name='" + name + '\'' +
    10. ", age=" + age +
    11. '}';
    12. }
    13. public Integer getId() {
    14. return id;
    15. }
    16. public void setId(Integer id) {
    17. this.id = id;
    18. }
    19. public String getName() {
    20. return name;
    21. }
    22. public void setName(String name) {
    23. this.name = name;
    24. }
    25. public Integer getAge() {
    26. return age;
    27. }
    28. public void setAge(Integer age) {
    29. this.age = age;
    30. }
    31. }

    PersonDao

    1. @Repository
    2. public interface PersonDao {
    3. public List getPersonList();
    4. }

    PersonService

    1. @Service
    2. public class PersonService {
    3. @Autowired
    4. private PersonDao personDao;
    5. public List getPersonList(){
    6. return personDao.getPersonList();
    7. };
    8. }

    TestController 

    1. @Controller
    2. public class TestController {
    3. @Autowired
    4. private PersonService personService;
    5. @RequestMapping(value = "/hi")
    6. public String hi() {
    7. System.out.println("TestController hi()");
    8. return "success";
    9. }
    10. @ResponseBody
    11. @RequestMapping(value = "/personList")
    12. public List personList(HttpServletRequest request, HttpServletResponse response) {
    13. List personList = personService.getPersonList();
    14. return personList;
    15. }
    16. }

    resource下创建mapper包

    在mapper下 

    编辑PersonMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.xg.dao.PersonDao">
    6. <select id="getPersonList" resultType="com.xg.pojo.Person">
    7. select * from person
    8. select>
    9. mapper>

    测试 

    http://localhost:8080/ssm/personList

     

  • 相关阅读:
    css绘制三角形
    Google Earth Engine(GEE)——提取sentinel-5p中NO2数据下载导出Google硬盘,可通过链接直接下载到电脑
    iRGD-PEG-SH 环肽iRGD聚乙二醇巯基 SH-PEG-iRGD iRGD-SH-PEG 的参数信息
    引入Wukong让你的系统瞬间具备IOC能力
    Leetcode刷题详解——不同路径
    利用 ASP.NET Core 开发单机应用
    leetcode 649. Dota2 参议院
    【STM32 IIC通信与温湿度传感器AHT20(I2C_AHT20)】
    项目控制有哪些关键?又何时实施项目控制?
    古玩交易NFT数字藏品平台
  • 原文地址:https://blog.csdn.net/m0_56195330/article/details/127739232