中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ssh框架搭建

發布時間:2020-08-05 20:48:59 來源:網絡 閱讀:344 作者:wx5d8484d25f876 欄目:編程語言

ssh框架搭建

目錄結構

ssh框架搭建

1.在pom.xml中添加如下依賴:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.5.RELEASE</spring.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <jackson.version>2.5.0</jackson.version>
</properties>

<dependencies>
    <!-- spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <!-- mysql連接 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.6</version>
    </dependency>
    <!-- log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!-- aop -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.4</version>
    </dependency>
    <!-- json -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

在build節點中添加如下代碼:

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

2.src/main/resources/datasource.properties

datasource.driverClassName=com.mysql.jdbc.Driver
datasource.userName=root
datasource.userPassword=123456
datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update

3.src/main/resources/log4j.properties

#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File

#定義日志輸出目的地為控制臺
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活的指定日志輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]-%m%n

#mybatis顯示SQL語句日志配置     net.cxp.blog.dao是你的dao接口所在的包名  
#log4j.logger.org.mybatis=DEBUG
log4j.logger.com.lymn.it.mapper=DEBUG

#文件大小到達指定尺寸的時候產生一個新的文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File=d:/logs/ssm.log
#定義文件最大大小
log4j.appender.File.MaxFileSize=10MB
#輸出所有日志,如果換成DEBUG表示輸出DEBUG以上級別日志
log4j.appender.File.Threshold=DEBUG
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n

4.src/main/resources/spring-mvc.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:p="http://www.springframework.org/schema/p"  
    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    
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自動掃描該包,使SpringMVC認為包下用了@controller注解的類是控制器,此處必須排除掉@Service組件 -->
    <context:component-scan base-package="com.lymn.it.controller" />
    <!-- 會自動注冊RequestMappingHandlerMapping與RequestMappingHandlerAdapter兩個Bean,這是Spring MVC
    為@Controller分發請求所必需的。啟動SpringMVC的注解功能,完成請求和注解POJO的映射 -->
    <mvc:annotation-driven/>

    <!-- 視圖解析器 -->
    <!-- 定義跳轉的文件的前后綴 ,視圖模式配置 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 這里的配置我的理解是自動給后面action的方法return的字符串加上前綴和后綴,變成一個 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--對靜態資源文件的訪問 -->
    <!-- 針對springMVC的restful風格的url而言,配置了 -->
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />

</beans>

5.src/main/resources/spring-hibernate.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
    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  
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 配置包含注解的掃描路徑,但是不包括controller -->
    <context:component-scan base-package="com.lymn.it.*">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- PropertyPlaceholderConfigurer 是 BeanFactory 后置處理器的實現,也是 BeanFactoryPostProcessor 
        接口的一個實現。 允許將上下文(配置文件)中的屬性值放在另一個單獨的標準 JavaProperties 文件中去。在 XML 文件中用類似 EL 
        表達式的 ${key} 替換指定的 properties 文件中的值 -->
    <!-- <bean id="placeHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location" value="classpath:config/datasource.properties"></property> 
        多個數據源寫法 <property name="locations"> <list> <value>classpath:config/datasource1.properties</value> 
        <value>classpath:config/datasource2.properties</value> </list> </property> 
        </bean> -->

    <!-- Spring2.5 以后,引入了簡化的引入外部文件的方式 -->
    <context:property-placeholder location="classpath:datasource.properties" />
    <!--Spring提供了的數據源,每個連接請求時都新建一個連接 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${datasource.driverClassName}"></property>
        <property name="url" value="${datasource.url}"></property>
        <property name="username" value="${datasource.userName}"></property>
        <property name="password" value="${datasource.userPassword}"></property>
    </bean>
    <!-- JNDI方式配置數據源 -->
    <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiName"> <value>java:comp/jndi/mysql</value> </property> 
        </bean> -->
    <!-- spring和hibernate完美整合 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 掃描entity包 使用別名 -->
        <property name="packagesToScan" value="com.lymn.it.model"></property>

        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <!--hibernate根據實體自動生成數據庫表-->
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <!--指定數據庫方言-->
                <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
                <!--在控制臺顯示執行的數據庫操作語句-->  
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <!--在控制臺顯示執行的數據哭操作語句(格式)-->     
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>     
            </props>
        </property>
    </bean>

    <!-- (事務管理)transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
    </bean>
     <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

6.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_3_1.xsd"
    version="3.1" metadata-complete="true">

    <!-- spring容器配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 注意,spring加載配置文件 -->
        <param-value>
            classpath:spring-hibernate.xml
        </param-value>
    </context-param>
    <!-- spring容器監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--springMVC的核心分發器 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定Spring的配置文件 -->
        <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>spring-mvc</servlet-name>
        <!-- 此處可以可以配置成*.do 適配Struts的習慣 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 防止Spring內存溢出監聽器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- 解決工程編碼過濾器 -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

7.DAO層之UserDao、UserDaoImpl

package com.lymn.it.dao;

import com.lymn.it.model.User;

public interface UserDao {
    public void save(User user);
    public User get(Integer uid);
}
package com.lymn.it.dao.impl;

import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.lymn.it.dao.UserDao;
import com.lymn.it.model.User;

@Repository
public class UserDaoImpl implements UserDao{
    @Resource
    SessionFactory sessionFactory;
    @Override
    public void save(User user) {
         Session session = sessionFactory.openSession();
         session.save(user);

    }
    @Override
    public User get(Integer uid) {
        Session session = sessionFactory.openSession();
        User object = (User) session.get(User.class, uid);
        return object;
    }

}

8.Service層之UserService、UserServiceImpl

package com.lymn.it.service;

import com.lymn.it.model.User;

public interface UserService {
    public void save(User user);
    public User get(Integer uid);
}
package com.lymn.it.service.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.lymn.it.dao.UserDao;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;

@Service
public class UserServiceImpl implements UserService{
    @Resource
    UserDao userDao;

    @Override
    public void save(User user) {
        userDao.save(user);
    }

    @Override
    public User get(Integer uid) {
        // TODO Auto-generated method stub
        return userDao.get(uid);
    }

}

9.Controller層之UserController

package com.lymn.it.controller;

import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;

@Controller
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping(value="/saveUser")
    public String saveUser(User user){
        userService.save(user);
        return "saveSucess";

    }
    @RequestMapping(value="/getUser")
    public String getUser(Map<String,Object> map){
        User user=userService.get(3);
        map.put("user", user);
        return "getSucess";

    }
}

10.Model層之User

package com.lymn.it.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {

    private Integer id;
    private String name;
    private String password;

    @Id
    @GeneratedValue
    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 String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", password=" + password
                + "]";
    }

}

jsp/getSucess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${user}</h2>
</body>
</html>

訪問如下圖所示代表成功
ssh框架搭建
ssh框架搭建
ssh框架搭建

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

手机| 礼泉县| 万州区| 稷山县| 西贡区| 乌兰察布市| 灌云县| 河北省| 德化县| 成安县| 旅游| 博兴县| 清徐县| 乐山市| 贵溪市| 普定县| 体育| 铜鼓县| 浦县| 长武县| 彰化市| 武陟县| 全州县| 大宁县| 临海市| 嘉峪关市| 峡江县| 松阳县| 黄浦区| 康平县| 营口市| 辽宁省| 潞西市| 岑溪市| 兖州市| 蓬溪县| 灵寿县| 自贡市| 北宁市| 微山县| 酉阳|