ssm_web project creation and configuration notes

SSM framework

SSM(spring、SpringMVC和Mybatis)

  1. Spring
    Spring is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container framework.
    Original intention
    1. JAVA EE development should be simpler.
    2. It is a better programming practice to use interfaces instead of classes. Spring reduces the complexity of using interfaces to almost zero.
    3. Provides a better application configuration framework for JavaBean.
    4. More emphasis on object-oriented design, rather than current technologies such as JAVA EE.
    5. Minimize unnecessary exception capture.
    6. Make the application easier to test.
    Spring's goals:
    1. It is convenient and pleasant to use Spring.
    2. The application code does not depend on Spring APIs.
    3. Spring does not compete with existing solutions, but is committed to integrating them.
    Spring's goals:
    1. It is convenient and pleasant to use Spring.
    2. The application code does not depend on Spring APIs.
    3. Spring does not compete with existing solutions, but is committed to integrating them.
  2. SpringMvc
    Springmvc is a module of the spring framework, spring and springmvc do not need to be integrated with an intermediate integration layer. Springmvc is a web framework based on mvc.
    Spring framework provides a full-featured MVC module for building web applications. Using Spring pluggable MVC architecture, when using Spring for WEB development, you can choose to use Spring's Spring MVC framework or integrate other MVC development frameworks.
  3. MyBatis
    MyBatis is an open source project iBatis of Apache, MyBatis is an excellent persistence layer framework, which supports custom SQL, stored procedures and advanced mapping. MyBatis eliminates almost all JDBC code and the work of setting parameters and obtaining result sets. MyBatis can configure and map primitive types, interfaces and Java POJOs (Plain Old Java Objects) as records in the database through simple XML or annotations.

Create web project based on SSM framework

  1. Create project
    Insert picture description here
  2. Import the relevant jar package in the pom file
    Insert picture description here
    Insert picture description here
  3. Create dao and entity files according to the generator.
    Insert picture description here
    Insert picture description here
    Generate files and codes. The generator.xml file is available on the
    Insert picture description here
    official website of the tutorial sequence:
    Insert picture description here
  4. Configuration file
    Insert picture description here
    application.xml configuration file:
<?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:tx="http://www.springframework.org/schema/tx"
       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">
    <!--包扫描-->
    <context:component-scan base-package="com.liu.service"/>
    <!--引入数据源的属性文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--数据源配置-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driverName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="initialSize" value="5"/>
        <property name="maxActive" value="20"/>
    </bean>
    <!--sessionFactory-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!--PageHelper分页插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            params=value1
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--为dao生成实现类型-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.liu.dao"/>
    </bean>
    <!--事务类-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
   <!--开启事务驱动-->
   <!--此处注意导包不要导错,结尾为tx的包为正确的包
   xmlns:tx="http://www.springframework.org/schema/tx"
   -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

springmvc.xml configuration file:

<?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">
    <!--包扫描-->
    <context:component-scan base-package="com.liu.controller"/>
    <!--静态资源放行-->
    <mvc:default-servlet-handler/>
    <!--开启驱动注解-->
    <mvc:annotation-driven/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

db.properties configuration file

jdbc.username=root
jdbc.password=密码
jdbc.driverName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3308/ssm_crud?characterEncoding=utf8&useSSL=false

Other documents are written by themselves

Guess you like

Origin blog.csdn.net/GUANGZHES/article/details/115261873