美文网首页
服务端:基础框架搭建

服务端:基础框架搭建

作者: 予你挚终_cc3a | 来源:发表于2018-01-23 11:44 被阅读0次

    (一)搭建框架

    我这里用的开发工具时idea,服务端用的框架时Spring+SpringMVC+mybatis框架,之前一直是在用这个框架但还没有自己真正搭建过这个框架,所有说这里就从最基本的搭建框架开始,我这里也是按照一个前辈的来搭建的。废话不多说了直接干吧。
    1.打开idea,创建maven项目,File-New-Project


    这里写图片描述

    2.选择Maven,勾选Create from archetype,选择org.apache.cocoon:cocoon-22-archetype-webapp,然后点击Next


    这里写图片描述
    3.填写Groupid和Artifactid点击Next
    这里写图片描述
    4.选择自己本地安装的maven
    这里写图片描述

    5.填写项目名创建项目完成


    这里写图片描述
    6.打开pom.xml加入坏境所需的依赖
    <?xml version="1.0" encoding="UTF-8"?>
    <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>ssm</groupId>
        <artifactId>com.cjq</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
        <dependency>
            <!--3.0的junit是使用编程的方式来进行测试,而junit4是使用注解的方式来运行junit-->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    
    
        <!--补全项目依赖-->
        <!--1.日志 java日志有:slf4j,log4j,logback,common-logging
            slf4j:是规范/接口
            日志实现:log4j,logback,common-logging
            使用:slf4j+logback
        -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!--实现slf4j接口并整合-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.1</version>
        </dependency>
    
        <!--1.数据库相关依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.1</version>
        </dependency>
    
        <!--2.dao框架:MyBatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!--mybatis自身实现的spring整合依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>
    
        <!--3.Servlet web相关依赖-->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    
        <!--4:spring依赖-->
        <!--1)spring核心依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!--2)spring dao层依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!--3)springweb相关依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!--4)spring test相关依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        </dependencies>
    </project>
    

    7.添加Java目录


    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述 这里写图片描述
    这里写图片描述 这里写图片描述

    8.添加需要的包


    这里写图片描述
    这里写图片描述

    9.建立资源文件


    这里写图片描述
    spring-dao.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 http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--配置整合mybatis过程
        1.配置数据库相关参数-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <!--2.数据库连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!--配置连接池属性-->
            <property name="driverClass" value="${driver}" />
    
            <!-- 基本属性 url、user、password -->
            <property name="jdbcUrl" value="${url}" />
            <property name="user" value="${username}" />
            <property name="password" value="${password}" />
    
            <!--c3p0私有属性-->
            <property name="maxPoolSize" value="30"/>
            <property name="minPoolSize" value="10"/>
            <!--关闭连接后不自动commit-->
            <property name="autoCommitOnClose" value="false"/>
    
            <!--获取连接超时时间-->
            <property name="checkoutTimeout" value="10000"/>
            <!--当获取连接失败重试次数-->
            <property name="acquireRetryAttempts" value="2"/>
        </bean>
    
        <!--约定大于配置-->
        <!--3.配置SqlSessionFactory对象-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--往下才是mybatis和spring真正整合的配置-->
            <!--注入数据库连接池-->
            <property name="dataSource" ref="dataSource"/>
            <!--配置mybatis全局配置文件:mybatis-config.xml-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <!--扫描entity包,使用别名,多个用;隔开-->
            <property name="typeAliasesPackage" value="com.cjq.vo"/>
            <!--扫描sql配置文件:mapper需要的xml文件-->
            <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        </bean>
    
        <!--4:配置扫描Dao接口包,动态实现DAO接口,注入到spring容器-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--注入SqlSessionFactory-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            <!-- 给出需要扫描的Dao接口-->
            <property name="basePackage" value="com.cjq.dao"/>
        </bean>
    </beans>
    

    spring-service.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" 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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!--扫描service包下所有使用注解的类型-->
        <context:component-scan base-package="com.cjq.service"/>
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--注入数据库连接池-->
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <bean id="date" class="java.util.Date"></bean>
        <!--配置基于注解的声明式事务
        默认使用注解来管理事务行为-->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>
    

    spring-web.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"
           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">
    
        <!--配置spring mvc-->
        <!--1,开启springmvc注解模式
        a.自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
        b.默认提供一系列的功能:数据绑定,数字和日期的format@NumberFormat,@DateTimeFormat
        c:xml,json的默认读写支持-->
        <mvc:annotation-driven/>
    
        <!--2.静态资源默认servlet配置-->
        <!--
            1).加入对静态资源处理:js,gif,png
            2).允许使用 "/" 做整体映射
        -->
        <mvc:default-servlet-handler/>
    
        <!--3:配置JSP 显示ViewResolver-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--4:扫描web相关的bean-->
        <context:component-scan base-package="com.cjq.controller"/>
    
        <mvc:resources mapping="/**/*.html" location="/"/>
        <mvc:resources mapping="/**/*.js" location="/"/>
        <mvc:resources mapping="/**/*.css" location="/"/>
        <mvc:resources mapping="/**/*.png" location="/"/>
        <mvc:resources mapping="/**/*.gif" location="/"/>
    </beans>
    

    jdbc.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    username=root
    password=admin
    

    mybatis-config.xml

    <?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>
        <!--配置全局属性-->
        <settings>
            <!--使用jdbc的getGeneratekeys获取自增主键值-->
            <setting name="useGeneratedKeys" value="true"/>
            <!--使用列别名替换列名  默认值为true
            select name as title(实体中的属性名是title) form table;
            开启后mybatis会自动帮我们把表中name的值赋到对应实体的title属性中
            -->
            <setting name="useColumnLabel" value="true"/>
            <!--开启驼峰命名转换Table:create_time到 Entity(createTime)-->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    </configuration>
    

    9.增加必要主要类


    这里写图片描述

    UserMapper

    package com.cjq.dao;
    
    import com.cjq.model.User;
    
    import java.util.List;
    
    /**
     * Created by struggle on 2017/7/26.
     */
    public interface UserMapper {
    
        List<User> getAll();
    
        void save(User user);
    
        void delete(int id);
    
        void update(User user);
    
        User getUserById(int id);
    
    }
    

    UserMapper.xml

    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.cjq.dao.UserMapper">
        <!--目的:为dao接口方法提供sql语句配置
        即针对dao接口中的方法编写我们的sql语句-->
    
        <select id="getAll" resultType="com.cjq.model.User">
            select * from user
        </select>
        <select id="getUserById" resultType="com.cjq.model.User" parameterType="int">
            select * from user where id=#{id}
        </select>
        <insert id="save" parameterType="com.cjq.model.User">
            insert into user(id,name,age,password) values(#{id},#{name},#{age},#{password})
        </insert>
        <delete id="delete" parameterType="int">
            delete from user where id=#{id}
        </delete>
        <update id="update" parameterType="com.cjq.model.User">
            update user set name=#{name},age=#{age},password=#{password} where id=#{id}
        </update>
    </mapper>
    

    UserService

    package com.cjq.service;
    
    import com.cjq.model.User;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    /**
     * Created by struggle on 2017/7/26.
     */
    @Transactional
    public interface UserService {
    
        List<User> getAll();
    
        void save(User user);
    
        void delete(int id);
    
        void update(User user);
    
        User getUserById(int id);
    }
    

    UserServiceImpl

    package com.cjq.service.impl;
    
    import com.cjq.dao.UserMapper;
    import com.cjq.model.User;
    import com.cjq.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * Created by struggle on 2017/7/26.
     */
    @Service("userService")
    public class UserServiceImpl implements UserService {
    
        @Autowired
        UserMapper userMapper;
        public List<User> getAll() {
            return userMapper.getAll();
        }
    
        public void save(User user) {
           userMapper.save(user);
        }
    
        public void delete(int id) {
          userMapper.delete(id);
        }
    
        public void update(User user) {
         userMapper.update(user);
        }
    
        public User getUserById(int id) {
            return userMapper.getUserById(id);
        }
    }
    

    10.使用Spring-test 测试
    UserServiceImplTest

    package com.cjq.servoce.impl;
    
    import com.cjq.model.User;
    import com.cjq.service.UserService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import javax.annotation.Resource;
    import java.util.Date;
    import java.util.List;
    
    /**
     * Created by struggle on 2017/7/26.
     */
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:spring/spring-*.xml")
    public class UserServiceImplTest {
        @Resource
        Date date;
        @Resource
        UserService userService;
    
        @Test
        public void testDate(){
            System.out.println(date);
        }
        @Test
        public void testGetAll(){
            List<User> list=userService.getAll();
            for(User user:list){
                System.out.println(user.getName());
            }
        }
        @Test
        public void testGetUserById(){
            System.out.println(userService.getUserById(1).getName());
        }
        @Test
        public void testSave(){
            User user=new User();
            user.setName("test");
            user.setAge(11);
            user.setPassword("pass");
            userService.save(user);
        }
        @Test
        public void testDelete(){
            userService.delete(1);
        }
        @Test
        public void testUpdate(){
            User user=userService.getUserById(2);
            user.setName("hello");
            userService.update(user);
        }
    }
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
      <!--用maven创建的web-app需要修改servlet的版本为3.1-->
      <!--配置DispatcherServlet-->
      <servlet>
        <servlet-name>cjq-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
            配置SpringMVC 需要配置的文件
            spring-dao.xml,spring-service.xml,spring-web.xml
            Mybites -> spring -> springMvc
        -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>cjq-dispatcher</servlet-name>
        <!--默认匹配所有请求-->
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    
      <welcome-file-list>
        <welcome-file>/login.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    好了基本上就是这样的了,我这个也是按照前辈的来的,转载出自
    这里,其实这个搭建起来还是挺容易的,按照这个来的我这里一点问题都没有,好了基本的服务端的框架算是搭建起来了

    相关文章

      网友评论

          本文标题:服务端:基础框架搭建

          本文链接:https://www.haomeiwen.com/subject/wafsaxtx.html