39.2、Maven实战

 

 

<?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.hhh</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!--Dependency Version Management-->

    <properties>

    <spring.version>5.2.4.RELEASE</spring.version>

    <mysql.driver.version>5.1.38</mysql.driver.version>

    <log4j.version>1.2.17</log4j.version>

    <aspectj.version>1.9.5</aspectj.version>

    <mybatis.version>3.5.4</mybatis.version>

    <mybatis-spring.version>2.0.3</mybatis-spring.version>

    <servlet-api.version>3.1.0</servlet-api.version>

    <jsp-api.version>2.2</jsp-api.version>

    <jstl.version>1.2</jstl.version>

    <tomcat-plugin.version>2.2</tomcat-plugin.version>

    <mybatis-generator.version>1.3.5</mybatis-generator.version>

</properties>

    <dependencyManagement>
    <dependencies>
    <!--Spring Dependency 使用spring的依赖-->
    <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>

    <!--MySQL Driver Dependency-->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.driver.version}</version>
    </dependency>

    <!--Log4j Dependency-->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
    </dependency>

    <!--AspectJ Dependency-->
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${aspectj.version}</version>
    </dependency>

    <!--MyBatis Dependency-->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>${mybatis.version}</version>
    </dependency>

    <!--MyBatis Integration Spring Dependency这是spring整合mybatis所依赖的-->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>${mybatis-spring.version}</version>
    </dependency>

    <!--Web Dependency-->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${servlet-api.version}</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>${jsp-api.version}</version>
    </dependency>


    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>${jstl.version}</version>
    </dependency>

    </dependencies>

</dependencyManagement>

<!--    下面就是配置的一些插件-->
    <build>
    <pluginManagement>
    <plugins>

    <!--Tomcat Plugin-->
    <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>${tomcat-plugin.version}</version>
    </plugin>

    <!--MyBatis Generator Plugin-->
    <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>${mybatis-generator.version}</version>
    </plugin>

</plugins>

</pluginManagement>

</build>

</project>

 

 

 配置插件的时候,不要忘记修改generatorConfig.xml文件时,添加一个DTD文件,并且修改路径问题:

 

 

 整合下列配置文件:

以下是连接数据库信息的db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hhh?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

 下面是applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util.xsd"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans">

    <!--解析properties配置文件-->
    <context:property-placeholder location="classpath:resource/db.properties"/>
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    <property value="${jdbc.driver}" name="driverClassName"/>
    <property value="${jdbc.url}" name="url"/>
    <property value="${jdbc.username}" name="username"/>
    <property value="${jdbc.password}" name="password"/>

    </bean>

    <!--配置SqlSessionFactory-->

    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
    <property name="dataSource" ref="dataSource"/>
    <property value="com.hhh.pojo" name="typeAliasesPackage"/>
    </bean>

    <!--配置MapperScanner-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScanner">
    <property value="com.hhh.mapper" name="basePackage"/>
    </bean>

</beans>

下面是applicatioContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans">

    <!--配置Spring包扫描-->
    <context:component-scan base-package="com.hhh.service"/>

</beans>

下面是application-trans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans">

    <!--配置事务管理器切面-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
    <property ref="dataSource" name="dataSource"/>

</bean>

    <!--配置事务传播行为-->

    <tx:advice id="txAdvice">
        <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>

    <tx:method name="modify*" propagation="REQUIRED"/>

    <tx:method name="drop*" propagation="REQUIRED"/>

    <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>

    <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>

    </tx:advice>

    <!--配置事务切点-->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* com.hhh.service.*.*(..))"/>
        <aop:advisor pointcut-ref="myPointcut" advice-ref="txAdvice"/>
    </aop:config>

</beans>

下面是springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>

        <beans 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans">

    <!--配置SpringMVC包扫描-->


    <context:component-scan base-package="com.hhh.web.controller"/>

    <!--配置注解驱动-->


    <mvc:annotation-driven/>

    <!--配置视图解析器-->



    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property value="/WEB-INF/jsp/" name="prefix"/>

    <property value=".jsp" name="suffix"/>

</bean>

</beans>

下面是web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="4.0"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">

    <!--指定Spring配置文件位置-->
    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring/applicationContext-*.xml</param-value>

</context-param>

    <!--配置启动Spring框架的监听器-->

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置SpringMVC主控制器-->
<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:spring/springmvc.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>

    <!--配置SpringMVC中的编码过滤器-->
 <filter>
    <filter-name>encodeFilter</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>encodeFilter</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

 

 创建添加业务service接口以及实现他的类:

 创建首页index.jsp:

 创建添加用户页面:

 创建成功页面ok.jsp

 创建页面跳转PageController控制器:

package com.hhh.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 页面跳转Controller
 */
@Controller
public class PageController {
    /**
     * 页面跳转
     */
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }
}

 创建添加用户控制器:

 

 创建查询用户的接口及实现类:(注意每次修改完service都要在maven插件里的parent重新清空clean然后install,最后在Controller里的tomcat run)

 创建查询用户的Controller

 创建用户显示页面:

<%--
  Created by IntelliJ IDEA.
  User: 21129
  Date: 2021/11/10
  Time: 0:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <table border="1" align="center">
        <tr>
            <th>用户名</th>
            <th>年龄</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${list}" var="user">
            <tr>
                <td>${user.username}</td>
                <td>${user.userage}</td>
                <td>
                    <a href="/users/preUpdateUser?userid=${user.userid}">更新</a>
                    <a href="/users/deleteUser?userid=${user.userid}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

 

 创建查询用户的接口及实现类:

 创建查询用户:

 创建显示预更新页面:

 

 创建更新的用户的接口及实现类:

 创建更新的方法在Controller:

 

 创建删除用户的接口及实现类:

package com.hhh.service;

import com.hhh.pojo.Users;

import java.util.List;

public interface UsersService {
    void addUser(Users users);
    List<Users> findUsers();
    Users findUserById(Integer userid);
    void modifyUsers(Users users);
    void dropUsersById(Integer userid);
}

接口实现类:

 

package com.hhh.service.impl;

import com.hhh.mapper.UsersMapper;
import com.hhh.pojo.Users;
import com.hhh.pojo.UsersExample;
import com.hhh.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户管理业务层
 */
@Service
public class UsersServiceImpl implements UsersService {
   @Autowired
   private UsersMapper usersMapper;

    /**
     * 实现用户添加业务
     * @param users
     */
    @Transactional
    @Override
    public void addUser(Users users) {
        this.usersMapper.insert(users);
    }

    /**
     * 查询用户
     * @return
     */
    @Override
    public List<Users> findUsers() {
        UsersExample example = new UsersExample();
        return this.usersMapper.selectByExample(example);
    }

    /**
     * 实现更新前的预更新,就是查询出符合条件的
     * @param userid
     * @return
     */
    @Override
    public Users findUserById(Integer userid) {
        return this.usersMapper.selectByPrimaryKey(userid);
    }

    /**
     * 更新用户
     * @param users
     */
    @Override
    @Transactional
    public void modifyUsers(Users users) {
        this.usersMapper.updateByPrimaryKey(users);
    }

    /**
     * 根据用户ID删除用户
     * @param userid
     * 因为他也是dml操作因此也要加一个@transactional
     */
    @Override
    @Transactional
    public void dropUsersById(Integer userid) {
        this.usersMapper.deleteByPrimaryKey(userid);
    }
}

创建删除用户COntroller:

 

package com.hhh.web.controller;

import com.hhh.pojo.Users;
import com.hhh.service.UsersService;
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 java.util.List;

/**
 * 处理用户管理请求
 */
@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;
    /**
     * 处理添加童虎请求
     */
    @RequestMapping("/addUsers")
    public String addUsers(Users users){
        this.usersService.addUser(users);
        return "redirect:/ok";
    }

    /**
     * 处理查询所有用户请求
     */
    @RequestMapping("/findUsers")
    public String findUsers(Model model){
        List<Users> list = this.usersService.findUsers();
        model.addAttribute("list",list);
        return "/showUsers";
    }
    /**
     * 处理预更新用户的查询
     */
    @RequestMapping("/preUpdateUser")
    public String preUpdateUser(Integer userid,Model model){
        Users users = this.usersService.findUserById(userid);
        model.addAttribute("user",users);
        return "/updateUsers";
    }
    /**
     * 更新用户
     */
    @RequestMapping("/updateUsers")
    public String updateUsers(Users users){
        this.usersService.modifyUsers(users);
        return "redirect:/ok";
    }
/**
 * 删除用户
 */
    @RequestMapping("/deleteUser")
    public String deleteUser(Integer userid){
        this.usersService.dropUsersById(userid);
        return "redirect:/ok";
    }
}

在浏览器输入:http://localhost:8080/index

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值