最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 進(jìn)階必備之ssm框架全面整合

 更新時間:2021年10月20日 08:29:28   作者:帶句晚安好嗎  
SSM框架是spring MVC ,spring和mybatis框架的整合,是標(biāo)準(zhǔn)的MVC模式,將整個系統(tǒng)劃分為表現(xiàn)層,controller層,service層,DAO層四層,使用spring MVC負(fù)責(zé)請求的轉(zhuǎn)發(fā)和視圖管理,spring實(shí)現(xiàn)業(yè)務(wù)對象管理,mybatis作為數(shù)據(jù)對象的持久化引擎

1.導(dǎo)入依賴

   <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!--數(shù)據(jù)庫驅(qū)動-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <!-- 數(shù)據(jù)庫連接池 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
 
    <!--Servlet - JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
 
    <!--Mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.2</version>
    </dependency>
 
    <!--Spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    <!--spring jdbc-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
 
    <!-- spring的核心依賴 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
 
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.1</version>
    </dependency>
          <!--json的依賴-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
 
</dependencies>
    <!-- <dependency>
         <groupId>com.fasterxml.jackson.core</groupId>
         <artifactId>jackson-databind</artifactId>
         <version>2.10.0</version>
     </dependency>-->
 
    <!--靜態(tài)資源問題-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2.創(chuàng)建實(shí)體類

package pojo;
 
public class books {
    private int bookId;
    private String bookName;
    private int bookCounts;
    private String detail;
 
    public books(int bookId, String bookName, int bookCounts, String detail) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }
 
    public books() {
    }
 
    public int getBookId() {
        return bookId;
    }
 
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
 
    public String getBookName() {
        return bookName;
    }
 
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
 
    public int getBookCounts() {
        return bookCounts;
    }
 
    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }
 
    public String getDetail() {
        return detail;
    }
 
    public void setDetail(String detail) {
        this.detail = detail;
    }
}

3.寫dao層接口

這里暫時只有一個方法

package Dao;
 
import pojo.books;
 
import java.util.List;
 
public interface BooksMapper {
    List<books> selectbooks();
}

4.寫mybatis核心配置文件和接口配置文件

這個是接口配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--把剛剛我們寫的接口配置進(jìn)去-->
<mapper namespace="Dao.BooksMapper">
 
<select id="BooksMapper" resultType="pojo.books">
    select * from books
</select>
 
 
</mapper>

mybatis核心配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--把那個接口導(dǎo)進(jìn)來-->
    <mappers>
        <package name="Dao"/>
    </mappers>
</configuration>

5.用spring整合Mybatis層也就是Dao層

這個是spring整合Mybatis的xml文件

<?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"
       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">
 
    <!-- 配置整合mybatis -->
    <!-- 1.關(guān)聯(lián)數(shù)據(jù)庫文件 -->
    <context:property-placeholder location="classpath:database.properties"/>
 
    <!-- 2.數(shù)據(jù)庫連接池 -->
    <!--數(shù)據(jù)庫連接池
        dbcp 半自動化操作 不能自動連接
        c3p0 自動化操作(自動的加載配置文件 并且設(shè)置到對象里面)
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置連接池屬性 -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
 
        <!-- c3p0連接池的私有屬性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 關(guān)閉連接后不自動commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 獲取連接超時時間 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 當(dāng)獲取連接失敗重試次數(shù) -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
 
    <!-- 3.配置SqlSessionFactory對象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入數(shù)據(jù)庫連接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
 
 
    <!--解釋 :https://www.cnblogs.com/jpfss/p/7799806.html-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 4.配置掃描Dao接口包,動態(tài)實(shí)現(xiàn)Dao接口注入到spring容器中 -->
        <property name="basePackage" value="Dao"/>
    </bean>
 
</beans>

數(shù)據(jù)庫配置文件,這里你們改一下數(shù)據(jù)庫就OK

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild
jdbc.username=root
jdbc.password=root

6.spring整合Service層

寫接口和實(shí)現(xiàn)類

package Service;
 
import org.springframework.stereotype.Service;
import pojo.books;
 
import java.util.List;
 
public interface BooksService {
        List<books> selectbooks();
    }
 

實(shí)現(xiàn)類

 @Service
 public class BooksServicelmpl implements BooksService{
   /*這里是把Dao的接口引進(jìn)來了因?yàn)镾ervice層調(diào)用Dao層*/
     @Autowired
    private BooksMapper booksMapper;
    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }
    @Override
    public List<books> selectbooks() {
        return booksMapper.selectbooks();
    }
}

寫spring配置文件

<?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"
       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:component-scan base-package="Service"/>
    <!-- <bean id="bookServicelmpl" class="service.bookServicelmpl">
     <property name="bookMapper" ref="bookMapper"/>
     </bean>-->
    <!--因?yàn)閟erv層要調(diào)Dao層我們把數(shù)據(jù)源拿過來,如果這里報紅就是因?yàn)闆]有引入Dao的那個數(shù)據(jù)源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
 
</beans>

關(guān)聯(lián)spring配置文件,我們寫個總的配置文件到進(jìn)去就ok

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <import resource="classpath:spring-Service.xml"/>
    <import resource="classpath:spring-Dao.xml"/>
</beans>

7.spring整合Conteoller層

<?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
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
    <!-- 掃描web相關(guān)的bean -->
    <context:component-scan base-package="Controller"/>
    <!-- 1.開啟SpringMVC注解驅(qū)動 -->
    <mvc:annotation-driven/>
    <!-- 2.靜態(tài)資源默認(rèn)servlet配置-->
    <mvc:default-servlet-handler/>
 
 
    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 后綴 -->
        <property name="suffix" value=".jsp"/>
    </bean>
 
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="failOnEmptyBeans" value="false"/>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
 
 
</beans>

寫Controller的類

@RestController
public class bookController {
    @Autowired
    private BooksService booksService;
    public void setBooksService(BooksService booksService) {
        this.booksService = booksService;
    }
    @RequestMapping("/books")
    public List<books> selectbooks()
    {
        List<books> list = booksService.selectbooks();
        return list;
    }
}

把三個配置文件關(guān)聯(lián)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <import resource="classpath:spring-Service.xml"/>
    <import resource="classpath:spring-Dao.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

8.添加web支持

寫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">
 
    <!--DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--一定要注意:我們這里加載的是總的配置文件,之前被這里坑了!-->
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
    <!--亂碼解決-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!--Session過期時間-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
 
</web-app>

創(chuàng)建一個lib包把依賴導(dǎo)進(jìn)去

添加工件測試

9.完整的目錄結(jié)構(gòu)

到此這篇關(guān)于Java 進(jìn)階必備之ssm框架全面整合的文章就介紹到這了,更多相關(guān)Java ssm框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

湟中县| 莱阳市| 南乐县| 张掖市| 陕西省| 双峰县| 顺昌县| 镇坪县| 平原县| 丰镇市| 广汉市| 武义县| 虹口区| 黄冈市| 成安县| 龙泉市| 丘北县| 南召县| 烟台市| 信丰县| 洛宁县| 什邡市| 梓潼县| 中方县| 奉化市| 无锡市| 德州市| 邵阳县| 鸡西市| 博客| 华阴市| 西青区| 外汇| 隆回县| 五峰| 阜康市| 留坝县| 阳谷县| 顺平县| 绥江县| 连城县|