美文网首页
搭建一个SSM环境

搭建一个SSM环境

作者: 明月几何8 | 来源:发表于2019-10-25 22:13 被阅读0次
    每日一新

    项目结构

    项目结构

    实体类TbMsg

    package com.zhuliming.entity;
    
    /**
     * 实体类
     */
    public class TbMsg {
    
        private int id;
        private String name;
        private String msg;
    
        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 getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        @Override
        public String toString() {
            return "TbMsg{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", msg='" + msg + '\'' +
                    '}';
        }
    }
    
    

    DAO层接口

    package com.zhuliming.dao;
    
    import com.zhuliming.entity.TbMsg;
    
    import java.util.List;
    
    /**
     * Dao层
     */
    public interface TbMsgDao {
        /**
         * 新增一条记录
         *
         * @param msg 实体对象
         * @return
         */
        public int addMsg(TbMsg msg);
    
        /**
         * 删除一条记录
         *
         * @param id id
         * @return
         */
        public int deleteMsg(int id);
    
        /**
         * 更新一条记录
         *
         * @param msg 实体对象
         * @return
         */
        public int updateMsg(TbMsg msg);
    
        /**
         * 查询全部
         *
         * @return
         */
        public List<TbMsg> queryAll();
    
    }
    

    DAO层映射文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.zhuliming.dao.TbMsgDao">
    
        <!--public int addMsg(TbMsg msg);-->
        <insert id="addMsg" parameterType="com.zhuliming.entity.TbMsg">
            insert into tb_msg(name, msg) values(#{name },#{msg})
        </insert>
        <!--public int deleteMsg(int id);-->
        <delete id="deleteMsg" parameterType="java.lang.Integer">
            delete from tb_msg where id = #{id}
        </delete>
        <!--public int updateMsg(TbMsg msg);-->
        <update id="updateMsg" parameterType="com.zhuliming.entity.TbMsg">
            update tb_msg set name=#{name }, msg=#{msg} where id=#{id}
        </update>
        <!--public List<TbMsg> queryAll();-->
        <select id="queryAll" resultType="com.zhuliming.entity.TbMsg">
            select id,name,msg from tb_msg
        </select>
    </mapper>
    
    

    Service层接口

    package com.zhuliming.service;
    
    import com.zhuliming.entity.TbMsg;
    
    import java.util.List;
    
    public interface TbMsgService {
        /**
         * 新增一条记录
         *
         * @param msg 实体对象
         * @return
         */
        public int addMsg(TbMsg msg);
    
        /**
         * 删除一条记录
         *
         * @param id id
         * @return
         */
        public int deleteMsg(int id);
    
        /**
         * 更新一条记录
         *
         * @param msg 实体对象
         * @return
         */
        public int updateMsg(TbMsg msg);
    
        /**
         * 查询全部
         *
         * @return
         */
        public List<TbMsg> queryAll();
    
    }
    

    Service层实现类

    package com.zhuliming.service.impl;
    
    import com.zhuliming.dao.TbMsgDao;
    import com.zhuliming.entity.TbMsg;
    import com.zhuliming.service.TbMsgService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    @Service
    public class TbMsgServiceImpl implements TbMsgService {
    
        @Autowired
        private TbMsgDao tbMsgDao;
    
        public int addMsg(TbMsg msg) {
            return tbMsgDao.addMsg(msg);
        }
    
        public int deleteMsg(int id) {
            return tbMsgDao.deleteMsg(id);
        }
    
        public int updateMsg(TbMsg msg) {
            return tbMsgDao.updateMsg(msg);
        }
    
        public List<TbMsg> queryAll() {
            return tbMsgDao.queryAll();
        }
    }
    

    Controller层

    package com.zhuliming.controller;
    
    import com.zhuliming.entity.TbMsg;
    import com.zhuliming.service.TbMsgService;
    import com.zhuliming.utils.RestResponse;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TbMsgController {
    
        @Autowired
        private TbMsgService tbMsgService;
    
        @RequestMapping("/hello")
        public String showHello() {
            return "HelloWorld";
        }
    
        @RequestMapping("/addMsg")
        public RestResponse addMsg(TbMsg msg) {
            int row = tbMsgService.addMsg(msg);
            if (row > 0) {
                return RestResponse.success();
            }
            return RestResponse.error();
        }
    }
    

    RestResponse工具类

    package com.zhuliming.utils;
    
    public class RestResponse {
    
        private int code;
        private String msg;
        private Object Data;
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public Object getData() {
            return Data;
        }
    
        public void setData(Object data) {
            Data = data;
        }
    
        public RestResponse(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public static RestResponse success(){
            return new RestResponse(0,"success");
        }
    
        public static RestResponse success(String msg){
            return new RestResponse(0,msg);
        }
    
        public static RestResponse error(){
            return new RestResponse(1,"error");
        }
    
        public static RestResponse error(String msg){
            return new RestResponse(0,msg);
        }
    }
    

    依赖的包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>com.zhuliming</groupId>
        <artifactId>SSM-Demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
    
            <!-- Spring WebMVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.9.RELEASE</version>
            </dependency>
    
            <!-- MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>
    
            <!-- MyBatis与Spring整合 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.2</version>
            </dependency>
    
            <!-- Spring JDBC,版本需要与spring-webmvc保持一致 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.3.9.RELEASE</version>
            </dependency>
    
            <!-- MySQL连接 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.13</version>
            </dependency>
    
            <!-- 数据库连接池 -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>
    
            <!-- Junit单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
            </dependency>
    
            <!-- Json转换器 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.10.0</version>
            </dependency>
    
        </dependencies>
    
    </project>
    
    

    配置文件application.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:util="http://www.springframework.org/schema/util"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:jee="http://www.springframework.org/schema/jee"
           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/util
            http://www.springframework.org/schema/util/spring-util.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/jee
            http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 注解驱动 -->
        <mvc:annotation-driven/>
        <!-- 组件扫描 -->
        <context:component-scan base-package="com.zhuliming"></context:component-scan>
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!-- 拦截器 -->
    <!--    <mvc:interceptors>
        <mvc:interceptor>
        <mvc:mapping path="/hello.do" />
        <bean class="cn.tedu.springmvc.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
        </mvc:interceptors>-->
    
        <!-- 读取db.properties配置文件 -->
        <util:properties id="db" location="classpath:db.properties"></util:properties>
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="#{db.driver}"></property>
            <property name="url" value="#{db.url}"></property>
            <property name="username" value="#{db.username}"></property>
            <property name="password" value="#{db.password}"></property>
            <property name="initialSize" value="#{db.initialSize}"></property>
            <property name="maxActive" value="#{db.maxActive}"></property>
        </bean>
        <!-- 指定数据源与XML映射的文件位置 -->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="mapperLocations" value="classpath:mappers/*.xml"></property>
        </bean>
        <!-- 指定接口文件在哪里 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
            <property name="basePackage" value="com.zhuliming.dao"></property>
        </bean>
    
    </beans>
    
    

    配置文件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">
    
        <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:application.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>
    </web-app>
    

    配置文件mapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.zhuliming.dao.TbMsgDao">
    
        <!--public int addMsg(TbMsg msg);-->
        <insert id="addMsg" parameterType="com.zhuliming.entity.TbMsg">
            insert into tb_msg(name, msg) values(#{name },#{msg})
        </insert>
        <!--public int deleteMsg(int id);-->
        <delete id="deleteMsg" parameterType="java.lang.Integer">
            delete from tb_msg where id = #{id}
        </delete>
        <!--public int updateMsg(TbMsg msg);-->
        <update id="updateMsg" parameterType="com.zhuliming.entity.TbMsg">
            update tb_msg set name=#{name }, msg=#{msg} where id=#{id}
        </update>
        <!--public List<TbMsg> queryAll();-->
        <select id="queryAll" resultType="com.zhuliming.entity.TbMsg">
            select id,name,msg from tb_msg
        </select>
    </mapper>
    

    配置文件db.properties

    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3306/db_case?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username=root
    password=root
    initialSize=3
    maxActive=10
    
    

    相关文章

      网友评论

          本文标题:搭建一个SSM环境

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