【ssh系列一】——框架搭建

来源:互联网 发布:服装图设计软件 编辑:程序博客网 时间:2024/06/13 05:33

一、建立项目(SpringMVC+Spring+hibernate的maven项目)





二、创建结构


三、添加pom文件内容

-<project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>maven</groupId>    <artifactId>maven</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>war</packaging>    <name/>    <description/>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!--<hibernate.version>5.2.10.Final</hibernate.version>-->        <hibernate.version>4.2.5.Final</hibernate.version>        <spring.version>4.3.10.RELEASE</spring.version>    </properties>    <dependencies>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>            <version>2.1</version>            <scope>provided</scope>        </dependency>        <!-- mysql数据库驱动 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.37</version>        </dependency>        <!-- aspectjweaver.jar这是Spring AOP所要用到的包 -->        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.7.1</version>        </dependency>        <!-- hibernate4 -->        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-core</artifactId>            <version>${hibernate.version}</version>        </dependency>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-entitymanager</artifactId>            <version>${hibernate.version}</version>        </dependency>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-ehcache</artifactId>            <version>${hibernate.version}</version>        </dependency>        <dependency>            <groupId>org.hibernate.javax.persistence</groupId>            <artifactId>hibernate-jpa-2.0-api</artifactId>            <version>1.0.1.Final</version>        </dependency>        <!-- spring mvc -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- spring3 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</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-jdbc</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-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-expression</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-orm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>            <exclusions></exclusions>        </dependency>        <!--jsp页面使用的jstl -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <artifactId>maven-war-plugin</artifactId>            </plugin>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.5</source>                    <target>1.5</target>                </configuration>            </plugin>        </plugins>    </build></project>-

四、添加相关配置文件内容

    1.web.xml

-<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"         xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <display-name></display-name>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <!-- spring hibernate -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring-hibernate.xml,classpath:spring.xml</param-value>    </context-param>    <!-- openSessionInView配置 -->    <filter>        <filter-name>openSessionInViewFilter</filter-name>        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>        <init-param>            <param-name>singleSession</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>openSessionInViewFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!--spring mvc 配置 -->    <servlet>        <servlet-name>spring-mvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <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>        <url-pattern>/</url-pattern>    </servlet-mapping>    <!-- encodeing -->    <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>    <!-- encoding filter for jsp page -->    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener></web-app>-

    2.spring-mvc.xml

-<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       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      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd      http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop.xsd      http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- 注解扫描包 -->    <!--影响返回之后的页面跳转-->    <context:component-scan base-package="com.ssh.controller" />    <!--影响页面的启动,dao和service都得添加,为什么?    是因为Controller通过@Autowired注入了service,然后service通过@Autowired注入了Dao    所以在编译加载的时候会通过注解进行寻找相关内容,所以需要将dao和service的路径都写上    (已经测试过了,将service中通过@Autowired注入dao的注释掉,将下面的dao路径删除,编译不会出错,    但是调用相关方法就会报找不到dao的类的错误)-->    <!--<context:component-scan base-package="com.ssh.dao,com.ssh.service" />-->    <!-- 开启mvc注解 -->    <mvc:annotation-driven />    <!--自动装配DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->    <mvc:default-servlet-handler />    <!--页面相关的控制内容-->    <bean id="viewResolver"          class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />        <property name="suffix" value=".jsp" />            <!-- 视图文件类型 -->        <property name="prefix" value="/WEB-INF/jsp" />  <!-- 视图文件的文件夹路径 -->    </bean></beans>-

    3.spring.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">    <!-- 自动扫描dao和service包(自动注入) -->    <context:component-scan base-package="com.ssh.dao,com.ssh.service" /></beans>-

    4.spring-hibernate.xml

-<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">    <!-- 引入属性文件 -->    <context:property-placeholder location="classpath:config.properties" />    <!-- 第二种形式加载properties配置文件 -->    <!--<bean id="propertyConfigurer"-->          <!--class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->        <!--<property name="locations">-->            <!--<list>-->                <!--<value>classpath:config.properties</value>-->            <!--</list>-->        <!--</property>-->    <!--</bean>-->    <!-- 配置数据源 -->    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >        <property name="url" value="${jdbc_url}" />        <property name="username" value="${jdbc_username}" />        <property name="password" value="${jdbc_password}" />        <property name="driverClassName" value="${driverClassName}"/>    </bean>    <!-- 配置hibernate session工厂 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="hibernateProperties">            <props>                <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>                <prop key="current_session_context_class">thread</prop>            </props>        </property>        <!-- 自动扫描注解方式配置的hibernate类文件 -->        <property name="packagesToScan">            <list>                <!-- 此处与entity实体路径对应 -->                <value>com.ssh.entity</value>            </list>        </property>    </bean>    <!-- 配置事务管理器 -->    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!-- 注解方式配置事物 -->    <tx:annotation-driven  transaction-manager="transactionManager" /></beans>-

    5.config.properties

-#\u5C5E\u6027\u6587\u4EF6#Oracle \u7684\u914D\u7F6E#hibernate.dialect=org.hibernate.dialect.OracleDialect#driverClassName=oracle.jdbc.driver.OracleDriver#validationQuery=SELECT 1 FROM DUAL#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl#jdbc_username=#jdbc_password=#SQLServer \u7684\u914D\u7F6E#hibernate.dialect=org.hibernate.dialect.SQLServerDialect#driverClassName=net.sourceforge.jtds.jdbc.Driver#validationQuery=SELECT 1#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy#jdbc_username=#jdbc_password=#Derby \u7684\u914D\u7F6E#hibernate.dialect=org.hibernate.dialect.DerbyDialect#driverClassName=org.apache.derby.jdbc.EmbeddedDriver#validationQuery=SELECT 1#jdbc_url=jdbc:derby:sy;create=true#jdbc_username=#jdbc_password=#MySQL \u7684\u914D\u7F6E#hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate_dialect=org.hibernate.dialect.MySQLDialect#hibernate_dialect=org.hibernate.dialect.MySQLMyISAMDialectdriverClassName=com.mysql.jdbc.DrivervalidationQuery=SELECT 1jdbc_url=jdbc:mysql://localhost:3306/sshtext?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNulljdbc_username=rootjdbc_password=roothibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=falsesessionInfoName=sessionInfouploadFieldName=filedatauploadFileMaxSize=20971520uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,miduploadDirectory=attached-

五、添加相关类

    1.entity

-package com.ssh.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * Created by mk on 2017/8/5. */@Entity(name="t_user")public class UserEntity {    @Id    @GeneratedValue    private int id;    private String name;    private String password;    public int getId() {        return id;    }    public void setId(int 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;    }}-

    2.Dao

-package com.ssh.dao;import com.ssh.entity.UserEntity;/** * Created by mk on 2017/8/5. */public interface UserDao {    public int insert(UserEntity userEntity);}-

    3.DaoImpl

-package com.ssh.dao.impl;import com.ssh.dao.UserDao;import com.ssh.entity.UserEntity;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;/** * Created by mk on 2017/8/5. */@Repositorypublic class UserDaoImpl implements UserDao {    @Autowired    SessionFactory sessionFactory;    public int insert(UserEntity userEntity) {        Session session = sessionFactory.getCurrentSession();        return (Integer)session.save(userEntity);    }}-

    4.Service

-package com.ssh.service;import com.ssh.entity.UserEntity;/** * Created by mk on 2017/8/5. */public interface UserService {    public int insert(UserEntity userEntity);}-

    5.ServiceImpl

-package com.ssh.service.impl;import com.ssh.dao.UserDao;import com.ssh.entity.UserEntity;import com.ssh.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * Created by mk on 2017/8/5. */@Servicepublic class UserServiceImpl implements UserService {    @Autowired    UserDao userDao;    public int insert(UserEntity userEntity) {        return userDao.insert(userEntity);    }}-

    6.Controller

-package com.ssh.controller;import com.ssh.entity.UserEntity;import com.ssh.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by mk on 2017/8/5. */@RequestMapping("/user1")@Controllerpublic class userController {    @Autowired    UserService userService;    @RequestMapping("/testindex")    public String inset(Model model){        UserEntity userEntity = new UserEntity();        userEntity.setName("1");        userEntity.setPassword("123");        int result = userService.insert(userEntity);        model.addAttribute("result",result);        return "/test";    }}-

    外.text.jsp

-<%--  Created by IntelliJ IDEA.  User: mk  Date: 2017/8/5  Time: 17:53  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body>    ${result}<br></body></html>-

六、建立数据库相关内容

    1.建立数据库

    2.建立表(和entity中的字段类型对应)

-CREATE TABLE `t_user` (  `id` int(100) NOT NULL AUTO_INCREMENT,  `name` varchar(255) DEFAULT NULL,  `password` varchar(255) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;-

七、配置tomcat



详细内容请参考:单击连接

八、运行测试

九、总结

    对于ssh有了一个整体的搭建框架的认识,接下来就是对每一个配置文件的剖析过程!

阅读全文
0 0
原创粉丝点击