当前位置: 首页>编程语言>正文

maven项目 集成 sonarqube maven整合ssm

Maven整合ssm框架

1 pom.xml

maven项目 集成 sonarqube maven整合ssm,maven项目 集成 sonarqube maven整合ssm_mybatis,第1张

<?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.itheima</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <!--

        ssm整合步骤:
        第一步:mybatis单独测试
            1.引入mybatis,mysql依赖
            2.创建实体和dao层。注意:dao层只有接口。
            3.创建dao层与之对象的mapper.xml配置文件
            4.创建mybatis的核心配置文件。
            5.通过api执行mybatis
                5.1 获取mybatis核心配置的io流对象
                5.2 创建SqlSessionFactory对象
                5.3 创建sqlSession对象
                5.4 创建dao层的代理对象。

       第二步: mybatis和spring进行整合
            整合的思想:使用spring的ioc的思想来管理mybatis中的对象。
            1.引入spring的ioc相关的依赖,还需要引入 spring和mybatis整合的工具包(依赖版本是1.3.0)   mybatis-spring
                还要包含 阿里巴巴的连接池,spring-jdbc,
            2.创建spring的配置文件,
            3.在spring的配置中创建SqlSessionFactory对象。创建数据源对象
            4.在mybatis中删除数据源相关的配置。
            5.采用包扫描的方式创建mapper对象

      第三步:单独测试springMVC
            1.引入springMVC的依赖  spring-webmvc
            2.在web.xml中配置前端控制器DispatcherServlet
               配置乱码过滤器
            3.创建springMVC.xml配置,配置注解扫描,注解驱动,视图解析器
            4.编写controller

      第四步:spring和springmvc整合
            整合的要点:就是让springmvc从servletContext中获取spring容器即可
            1.在web.xml中配置spring的监听器即可。

    -->

    <build>
        <plugins>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!--junit测试依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- spring的ioc相关-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- mybatis-spring整合的工具包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- spring整合junit测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- 德鲁伊连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- spring的jdbc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!--mvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- jstl依赖-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--aop相关-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>
    </dependencies>


</project>

2 web.xml

maven项目 集成 sonarqube maven整合ssm,maven项目 集成 sonarqube maven整合ssm_spring_02,第2张

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">


    <!-- 前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置springmvc.xml的位置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc/springMVC.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 乱码过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- spring的监听器-->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

3 jdbc.properties

jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/spring_day01
jdbc.driverClass=com.mysql.jdbc.Driver

4 springMVC.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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注解扫描-->
    <context:component-scan base-package="com.itheima.controller"></context:component-scan>

    <!-- 注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 释放自静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

5 applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- bean definitions here -->
    <!-- 引入外部配置 -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!-- 创建数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
    </bean>
    
    <!-- 开启包扫描-->
    <context:component-scan base-package="com.itheima">
        <!-- 排除springmvc创建的controller注解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>
    
</beans>

6 applicationContext-mybatis.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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- bean definitions here -->

    <!--
       思考一个问题:
           此处的sqlSessionFactory的对象的创建属于我们spring中已经学习的3种方式的哪一种?
           1.默认无参构造
           2.工厂类的静态方法
           3.工厂类的动态方法

           4.FactoryBean方式。
            如果一个类实现了spring的FactoryBean接口,那么spring就会调用这个接口的
            getObject()方法来创建对象。
   -->

    <!--
        创建sqlSessionFactory对象
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置mybatis核心配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
        <!-- 别名映射,mybatis中就不需要配置了-->
        <property name="typeAliasesPackage" value="com.itheima.domain"></property>
        <!-- 配置mapper映射文件的位置,mybaits中就不需要配置了 -->
        <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"></property>
    </bean>

    <!-- spring管理单个mapper对象-->
    <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
    <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>-->
    <!--<property name="mapperInterface" value="com.itheima.mapper.UserMapper"></property>-->
        
    <!--</bean>-->

    <!-- spring通过包扫描的方式,管理所有的mapper对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>

7 applicationContext-tx.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- bean definitions here -->

    <!--
      xml版本 spring配置事务
      1.创建一个事务管理器对象
      2.配置事务的策略
      3.配置事务的aop
  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--
    <!--配置一个通知(增强)-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--配置哪些方法需要被增强, 设置隔离级别  传播行为 是否只读  超时时间
               name="transfer*"  设置要事务管理进行增强的方法
               isolation="REPEATABLE_READ"  设置隔离级别
               propagation="REQUIRED" 设置传播行为
               read-only="false" 设置是否为只读事务
               timeout="-1"  超时时间为-1 不超时
            -->
            <tx:method name="transfer*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="-1" />
        </tx:attributes>
    </tx:advice>

    <!--配置aop-->
    <aop:config>
        <!--配置切点-->
        <aop:pointcut id="pointcut" expression="execution(* com.itheima.service..transfer*(..))"/>
        <!--切点和增强的组合-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>-->

    <!--
        注解方式
        1.开启注解事务
        2.在类名上添加@Transactional注解即可
    -->
    <tx:annotation-driven></tx:annotation-driven>
</beans>

8 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>


    <!-- 引入外部配置  -->

    <!--<properties resource="jdbc.properties"></properties>-->

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!-- 别名映射,交给spring管理,所以注释掉-->
   <!-- <typeAliases>
        <package name="com.itheima.domain"></package>
    </typeAliases>-->

    <!-- 数据源交给spring管理-->
    <!--<environments default="development">-->
        <!--<environment id="development">-->
            <!--<transactionManager type="JDBC"/>-->
            <!--<dataSource type="POOLED">-->
                <!--<property name="driver" value="${jdbc.driverClass}"/>-->
                <!--<property name="url" value="${jdbc.url}"/>-->
                <!--<property name="username" value="${jdbc.username}"/>-->
                <!--<property name="password" value="${jdbc.password}"/>-->
            <!--</dataSource>-->
        <!--</environment>-->
    <!--</environments>-->
    <!-- 交给spring管理-->
   <!-- <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>-->
</configuration>

9 UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
   mapper标签:配置各类声明
   namespace:名称空间,由于映射文件可能有多个,为了防止crud语句的唯一标识被重复,需要设置空间名称。
 -->

<mapper namespace="com.itheima.mapper.UserMapper">
    <!--
        select:查询的statement(声明),用来编写查询语句
        id:语句的唯一标识
        resultType:配置返回的结果集类型
        parameterType:传递的参数类型,可以省略
    -->

    <select id="findAllUsers" resultType="User">
        select * from tb_user
    </select>

    <insert id="save">
        insert into tb_user(name,password) values(#{name},#{password})
    </insert>

    <delete id="deleteUserById">
        delete from tb_user where id = #{id}
    </delete>

</mapper>

10 contoller

package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.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;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 此时注入不进来的原因是因为springmvc拿不到spring容器。
     *
     *  所以spring和springmvc的整合核心是:
     *      只需要让springmvc拿到spring的容器即可。
     *
     *  所以将spring容器放到servletContext域中,springmvc从servletContext域中获取spring容器
     *
     *  说白了,我们只要配置spring的监听器,让spring介入到web工程中,那么就能实现spring和springmvc的整合了
     *
     */
    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUsers")
    public String findAllUsers(Model model){

        List<User> userList = userService.findAllUsers();

        //保存数据
        model.addAttribute("userList",userList);
        return "users";
    }


    @RequestMapping("/addTwoUsers")
    public String addTwoUsers(){

        userService.addTwoUsers();

        return "redirect:/user/findAllUsers";
    }


    @RequestMapping("/deleteUserById")
    public String deleteUserById(String id){

        userService.deleteUserById(id);

        return "redirect:/user/findAllUsers";
    }
}

11 User

package com.itheima.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    // 用户名
    private String userName;
    // 密码
    private String password;
    // 姓名
    private String name;
    // 年龄
    private Integer age;
    // 性别,1男性,2女性
    private Integer sex;
    // 出生日期
    private Date birthday;
    // 创建时间
    private Date created;
    // 更新时间
    private Date updated;
}

12 UserMapper

package com.itheima.mapper;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {
    
    public List<User> findAllUsers();

    void save(User user);

    void deleteUserById(@Param("id") String id);
}

13 UserService

package com.itheima.service;

import com.itheima.domain.User;

import java.util.List;

public interface UserService {

    public List<User> findAllUsers();

    void addTwoUsers();

    void deleteUserById(String id);
}
package com.itheima.service.impl;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAllUsers() {
        List<User> userList = userMapper.findAllUsers();

        return  userList;
    }

    @Override
    public void addTwoUsers() {

        User user = new User();
        user.setPassword("19951001");
        user.setName("沈腾");

        User user2 = new User();
        user2.setPassword("199510012");
        user2.setName("沈腾2");

        userMapper.save(user);
        int i = 1/0;
        userMapper.save(user2);

    }

    @Override
    public void deleteUserById(String id) {
        userMapper.deleteUserById(id);
    }
}

14 utils

package com.itheima.utils;

import com.itheima.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapperUtils {


    private SqlSessionFactory sqlSessionFactory;
    private Class calzz;
    public Object getMapper(){
        //3.通过sqlSessionFactory创建sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.通过sqlSession获取Mapper对象
        Object mapper = sqlSession.getMapper(calzz);
        return mapper;
    }


    // 一步到位创建所有的mapper对象
    public Map<String,Object> getAllMappers(){

        //获取所有mapper的字节码文件
        List<Class> classList = new ArrayList<>();
        for(Class clazz:classList){
            //3.通过sqlSessionFactory创建sqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            //4.通过sqlSession获取Mapper对象
            Object mapper = sqlSession.getMapper(calzz);
        }
        return new HashMap<>();
    }
}

15 user.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: zhang
  Date: 2018/7/25
  Time: 20:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" rel="stylesheet" href="/js/user.css"/>
</head>
<body>

<!-- Table goes in the document BODY -->
<table class="altrowstable" id="alternatecolor">
    <tr>
        <th>用户编号</th>
        <th>用户名</th>
        <th>真实姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>出生日期</th>
        <th>创建日期</th>
        <th>更新日期</th>
        <th>删除操作</th>
    </tr>
    <c:forEach items="${userList}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.userName}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
            <td>${user.sex}</td>
            <td><f:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"></f:formatDate></td>
            <td><f:formatDate value="${user.updated}" pattern="yyyy-MM-dd hh:mm:ss"></f:formatDate></td>
            <td><f:formatDate value="${user.created}" pattern="yyyy-MM-dd hh:mm:ss"></f:formatDate></td>
            <td><a href="/user/deleteUserById?id=${user.id}">删除</a></td>
        </tr>
    </c:forEach>
</table>
</body>
</html>



https://www.xamrdz.com/lan/5j41921816.html

相关文章: