使用idea工具 maven整合 ssm

喜你入骨 提交于 2020-03-14 15:35:20
<?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">
    
    <!--核心监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!--spring配置文件的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    
    <!--前端控制器-->
    <servlet>
        <servlet-name>sprin-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>
    </servlet>
    <servlet-mapping>
        <servlet-name>sprin-mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!--解决post中文乱码的过滤器-->
    <filter>
        <filter-name>characterEncoding</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>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <welcome-file-list>
        <welcome-file>/listAll.do</welcome-file>
    </welcome-file-list>

</web-app>
web.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>aaa</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.18.RELEASE</spring.version>
    </properties>

    <!--声明项目依赖的jar包-->
    <dependencies>
        <!--groupId配置发布jar包的公司的名称-->
        <!--artifactId 配置jar包名称-->
        <!-- version配置jar包的版本-->
        <!-- spring核心包 -->
        <!-- 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-web</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-jdbc</artifactId>
            <version>${spring.version}</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>

        <!--maybatis的jar包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>6.0</version>
        </dependency>




        <!-- 文件上传 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>
                        **/*.xml
                    </include>
                </includes>
            </resource>
        </resources>
    </build>

</project>
pom.xml
写包和实体类



在dao层service和controller层写业务逻辑代码
package com.aaa.ssm.dao;

import java.util.List;
import java.util.Map;

public interface ClassDao {
    /**
     * 查询班级表所有信息
     * @return
     */
    public List<Map> listAll();

    /**
     * 根据classid删除
     * @param classid
     */
    public void delete(int classid);

    /**
     * 添加对象
     * @param map
     */
    public void add(Map map);

    /**
     * 查询tb_class的班级名
     */
    public List<Map> findGradeName();

    /**
     * 模糊查询
     */
    public List<Map> search(Map map);

    /**
     * 获取要修改的对象
     * @param map
     * @return
     */
    public List<Map> getClass(Map map);

    /**
     * 修改
     */
    public void update(Map map);
}
dao层
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.ssm.dao.ClassDao">
    <!--查询所有-->
    <select id="listAll" resultType="map">
        select g.gradeid,g.gradename,c.classid,c.classname,c.classsize,c.remark from tb_class c inner join tb_grade g on g.gradeid=c.gradeid
    </select>

    <!--删除-->
    <delete id="delete">
        delete from tb_class where classid=#{classid}
    </delete>

    <!--查询gradeName-->
    <select id="findGradeName" resultType="map">
        select gradeid,gradename from tb_grade
    </select>

    <!--添加-->
    <insert id="add">
        insert into tb_class values(null,#{classname},#{classsize},#{gradeid},#{remark})
    </insert>

    <!--模糊查询-->
    <select id="search" resultType="map">
        select g.gradeid,g.gradename,c.classid,c.classname,c.classsize,c.remark from tb_class c inner join tb_grade g on c.gradeid=g.gradeid where c.classname like '%${classname}%' or c.remark like '%${classname}%'
    </select>

    <!--获取要修改的对象-->
    <select id="getClass" resultType="map">
        select * from tb_class where classid=#{classid}
    </select>

    <!--修改-->
    <update id="update">
        update tb_class set classname=#{classname},classsize=#{classsize},gradeid=#{gradeid},remark=#{remark} where classid=#{classid}
    </update>

</mapper>
dao的实现Mapper文件
package com.aaa.ssm.service;

import java.util.List;
import java.util.Map;

public interface ClassService {
    /**
     * 查询班级表所有信息
     * @return
     */
    public List<Map> listAll();

    /**
     * 根据classid删除
     * @param classid
     */
    public void delete(int classid);

    /**
     * 添加
     * @param map
     */
    public void add(Map map);

    /**
     * 查询tb_class的班级名
     */
    public List<Map> findGradeName();

    /**
     * 模糊查询
     */
    public List<Map> search(Map map);

    /**
     * 获取要修改的对象
     * @param map
     * @return
     */
    public List<Map> getClass(Map map);

    /**
     * 修改
     */
    public void update(Map map);
}
Service层
package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.ClassDao;
import com.aaa.ssm.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassDao classDao;

    /**
     * 查所有
     * @return
     */
    public List<Map> listAll() {
        return classDao.listAll();
    }

    /**
     * 删除
     * @param classid
     */
    public void delete(int classid) {
        classDao.delete(classid);
    }

    /**
     * 添加
     * @param map
     */
    public void add(Map map) {
       classDao.add(map);
    }

    /**
     * 查询tb_grade表中所有复选按钮
     * @return
     */
    public List<Map> findGradeName() {
        List<Map> gradeName = classDao.findGradeName();
        return gradeName;
    }

    /**
     * 模糊查询
     * @param map
     * @return
     */
    public List<Map> search(Map map) {
        List<Map> mapList = classDao.search(map);
        return mapList;
    }

    /**
     * 获取要更新的对象
     * @param map
     * @return
     */
    public List<Map> getClass(Map map) {
        List<Map> aClass = classDao.getClass(map);
        if (aClass!=null){
            return aClass;
        }else{
            return null;
        }

    }

    /**
     * 更新
     * @param map
     */
    public void update(Map map) {
        classDao.update(map);
    }
}
service实现类

 

package com.aaa.ssm.controller;

import com.aaa.ssm.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class ClassController {
    @Autowired
    private ClassService classService;

    /**
     * 查询所有
     * @param model
     * @return
     */
    @RequestMapping("/listAll")
    public String listAll(Model model){
        List<Map> listAll = classService.listAll();
        model.addAttribute("listAll",listAll);
        return "list";
    }
    /**
     * 根据classid删除
     */
    @RequestMapping("/delete")
    public String delete(Integer classid){
        classService.delete(classid);
        return "redirect:/listAll.do";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Map> gradeName = classService.findGradeName();
        model.addAttribute("gradeName",gradeName);
        return "add";
    }
    /**
     * 添加
     * @return
     */
    @RequestMapping("/add")
    public String add(@RequestParam Map map){
        classService.add(map);
        System.out.println(map);
        return "redirect:/listAll.do";
    }

    /**
     * 模糊查询
     */
    @RequestMapping("/search")
    public String search(@RequestParam Map map,String classname,Model model){
        List<Map> classList = classService.search(map);
        model.addAttribute("listAll",classList);
        model.addAttribute("classname",classname);
        return "list";
    }

    /**
     * 去更新界面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam Map map,Model model){
        List<Map> aClass = classService.getClass(map);
        List<Map> gradeName = classService.findGradeName();
        Map classMap = aClass.get(0);
        model.addAttribute("classMap",classMap);
        model.addAttribute("gradeName",gradeName);
        return "toUpdate";
    }

    /**
     * 更新
     */
    @RequestMapping("/update")
    public String update(@RequestParam Map map){
        classService.update(map);
        return "redirect:/listAll.do";
    }

    /**
     * 返回主页
     */
    @RequestMapping("/toReturn")
    public String toReturn(){
        return "redirect:/listAll.do";
    }
}
controller层

 

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root
连接数据库的properties文件jdbc.properties
<?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">

    <!--扫描包-->
    <context:component-scan base-package="com.aaa.ssm.service"></context:component-scan>
    <!--读取properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--声明数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <!--配置工厂类对象-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
   <!--使用哪个dao包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.aaa.ssm.dao"></property>
    </bean>
    <!--配置声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--以注解的方式配置声明式事务-->
    <tx:annotation-driven></tx:annotation-driven>
</beans>
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"
       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">

    <context:component-scan base-package="com.aaa.ssm.controller"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
spring-mvc的配置文件
运行结果


 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!