整合SSM框架注解版

目录

    • 1、在ssm配置版的基础上修改成注解版并完成CRUD
    • 2、修改applicationContext.xml的配置,添加如下配置
    • 3、编写UserServiceImpl实现类添加注解
    • 4、编写控制类UserController添加注解
    • 5、编写jsp页面
    • 6、运行效果

1、在ssm配置版的基础上修改成注解版并完成CRUD

项目结构如下
整合SSM框架注解版_第1张图片
配置版在如下链接

https://blog.csdn.net/weixin_48500300/article/details/118030142

2、修改applicationContext.xml的配置,添加如下配置

    
    <mvc:annotation-driven />
    
    <mvc:default-servlet-handler />
    
    <context:component-scan base-package="ltl"/>

完整配置如下


<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:apo="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/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

    
    <context:property-placeholder location="classpath:db.properties"/>

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        
        <property name="maxPoolSize" value="30"/>
        
        <property name="minPoolSize" value="10"/>
        
        <property name="autoCommitOnClose" value="false"/>
        
        <property name="checkoutTimeout" value="10000"/>
        
        <property name="acquireRetryAttempts" value="2"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="ltl.mapper"/>
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    bean>

    
    <mvc:annotation-driven />
    
    <mvc:default-servlet-handler />
    
    <context:component-scan base-package="ltl"/>

3、编写UserServiceImpl实现类添加注解

package ltl.service.impl;

import ltl.domain.User;
import ltl.mapper.UserMapper;
import ltl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    //service调dao层
    @Autowired
    private UserMapper userMapper;


    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUserById(int id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public User queryUserById(int id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }
}

4、编写控制类UserController添加注解

package ltl.controller;

import ltl.domain.User;
import ltl.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 java.util.List;

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

    @Autowired
    private UserService userService;

    //进入用户信息页
    @RequestMapping("/allUser")
    public String allUser(Model model){
        List<User> list =  userService.queryAllUser();
        model.addAttribute("list", list);
        //跳转到allUser.jsp
        return "allUser";
    }

    //添加
    @RequestMapping("/addUser")
    public String addUser(User user){
        System.out.println("addUser=>" + user);
        int i = userService.addUser(user);
        //重定向到/user/allUser请求
        return "redirect:/user/allUser";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(int id, Model model) {
        User user = userService.queryUserById(id);
        model.addAttribute("user", user);
        //跳转到updateUser.jsp
        return "updateUser";
    }

    //修改用户信息
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        int i =  userService.updateUser(user);
        if (i > 0) {
            System.out.println("修改用户信息成功" + user);
        }
        //重定向到/user/allUser请求
        return "redirect:/user/allUser";
    }

    //删除用户
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        int i = userService.deleteUserById(id);
        if(i>0){
            System.out.println("删除用户成功");
        }
        //重定向到/user/allUser请求
        return "redirect:/user/allUser";
    }


}

5、编写jsp页面

1、index.jsp如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!h2>

<h3>
    <a href="${pageContext.request.contextPath}/user/allUser">进入书籍页面a>
h3>

body>
html>

2、allUser.jsp如下

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户信息title>
head>
<body>
<table>
    <thead>
    <tr>
        <th>用户编号th>
        <th>用户名称th>
        <th>用户密码th>
        <th>用户邮箱th>
        <th>操作th>
    tr>
    thead>

    <tbody>
    <c:forEach var="user" items="${list}">
        <tr>
            <td>${user.id}td>
            <td>${user.username}td>
            <td>${user.password}td>
            <td>${user.email}td>
            <td>
                <a href="${pageContext.request.contextPath}/user/toUpdate?id=${user.id}">修改a>
                  |  
                <a href="${pageContext.request.contextPath}/user/deleteUser?id=${user.id}">删除a>
            td>
        tr>
    c:forEach>
    tbody>
table>

<form action="${pageContext.request.contextPath}/user/addUser" method="post">
    <label>用户名label>
    <input type="text" name="username"  required>
    <br>
    <label>密码label>
    <input type="text" name="password"  required>
    <br>
    <label>邮箱label>
    <input type="text" name="email"  required>
    <br>
    <input type="submit" value="添加">
form>

body>
html>

3、updateUser.jsp如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改用户信息title>
head>
<body>
<form action="${pageContext.request.contextPath}/user/updateUser" method="post">
    <input type="hidden" name="id" value="${user.id}">
    <label>用户名label>
    <input type="text" name="username" value="${user.username}" required>
    <br>
    <label>密码label>
    <input type="text" name="password" value="${user.password}" required>
    <br>
    <label>邮箱label>
    <input type="text" name="email" value="${user.email}" required>
    <br>
    <input type="submit" value="修改">
form>
body>
html>

6、运行效果

整合SSM框架注解版_第2张图片
整合SSM框架注解版_第3张图片
整合SSM框架注解版_第4张图片

你可能感兴趣的:(SSM框架,java,idea,maven,ssm)