SpringMVC 整合

2021-04-13 06:27

阅读:397

标签:root   import   public   bsp   sql   char   hot   inter   end   

一、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.0modelVersion>

    groupId>com.wtgroupId>
    artifactId>ssmbuildartifactId>
    version>1.0-SNAPSHOTversion>

    dependencies>
        
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
            version>4.10version>
        dependency>
        
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
            version>5.1.6version>
        dependency>
        
        dependency>
            groupId>com.mchangegroupId>
            artifactId>c3p0artifactId>
            version>0.9.5.2version>
        dependency>
        
        dependency>
            groupId>javax.servletgroupId>
            artifactId>servlet-apiartifactId>
            version>2.5version>
            scope>providedscope>
        dependency>
        dependency>
            groupId>javax.servlet.jspgroupId>
            artifactId>jsp-apiartifactId>
            version>2.2version>
            scope>providedscope>
        dependency>
        dependency>
            groupId>javax.servletgroupId>
            artifactId>jstlartifactId>
            version>1.2version>
        dependency>
        
        dependency>
            groupId>org.mybatisgroupId>
            artifactId>mybatisartifactId>
            version>3.4.6version>
        dependency>
        dependency>
            groupId>org.mybatisgroupId>
            artifactId>mybatis-springartifactId>
            version>2.0.5version>
        dependency>
        
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-webmvcartifactId>
            version>5.2.0.RELEASEversion>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-jdbcartifactId>
            version>5.2.0.RELEASEversion>
        dependency>

        
        dependency>
            groupId>com.fasterxml.jackson.coregroupId>
            artifactId>jackson-databindartifactId>
            version>2.11.0version>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>fastjsonartifactId>
            version>1.2.60version>
        dependency>

    dependencies>
    
    build>
        resources>
            resource>
                directory>src/main/javadirectory>
                includes>
                    include>**/*.xmlinclude>
                includes>
            resource>
        resources>
    build>
project>

二、整合mybatis

1、resource资源文件夹下,创建 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>
    
    typeAliases>
        package name="com.wt.pojo"/>
    typeAliases>
    
    mappers>
        mapper class="com.wt.mapper.BookMapper"/>
    mappers>
configuration>

2、resource资源文件夹下, 创建spring的配置文件 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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


beans>

3、创建数据库连接信息 db.properties (注意:数据库通过 spring连接)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?characterEncoding=UTF8
name=root
password=@WSX3edc

4、创建

com.wt.pojo

com.wt.mapper

com.wt.server

com.wt.controller

5、pojo

创建Books.java

package com.wt.pojo;

public class Books {
    private int bookID;
    private String bookName;
    private int  bookCounts;
    private String detail;

    public Books() {
    }

    public Books(int bookID, String bookName, int bookCounts, String detail) {
        this.bookID = bookID;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @Override
    public String toString() {
        return "Books{" +
                "bookID=" + bookID +
                ", bookName=‘" + bookName + ‘\‘‘ +
                ", bookCounts=" + bookCounts +
                ", detail=‘" + detail + ‘\‘‘ +
                ‘}‘;
    }
}

6、mapper层

BookMapper接口

package com.wt.mapper;

import com.wt.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.awt.print.Book;
import java.util.List;

public interface BookMapper {
    //
    int addBook(Books books);
    //
    int deleteBook(@Param("id") int id);
    //
    int updateBook(@Param("id") int id);
    //
    int selectBook(@Param("id") int id);
    // 查所有
    List getBooksList();
}

添加 mybatis配置文件的 别名 和 mapper

BooKMapper.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="com.wt.mapper.BookMapper">
    insert id="addBook" parameterType="books">
        insert into books(bookName, bookCounts, detail) values (
        #{bookName}, #{bookCounts} #{detail}
        )
    insert>

    delete id="deleteBook" parameterType="int">
        delete from books where bookID = #{id}
    delete>

    update id="updateBook" parameterType="int">
        update books
        set
        bookName = #{bookName},
        bookCounts = #{bookCounts},
        detail = #{detail}
        where bookID = #{id}
    update>

    select id="selectBook" parameterType="int" resultType="books">
        select * from books where bookID = #{id}
    select>

    select id="getBooksList" resultType="books">
        select  * from books
    select>
mapper>

7、service(层)

BookService 接口类

package com.wt.service;

import com.wt.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.awt.print.Book;
import java.util.List;

public interface BookService {
    //
    int addBook(Books books);
    //
    int deleteBook(int id);
    //
    int updateBook(int id);
    //
    int selectBook(int id);
    // 查所有
    List getBooksList();
}

BookServiceImpl.java

package com.wt.service;

import com.wt.mapper.BookMapper;
import com.wt.pojo.Books;

import java.awt.print.Book;
import java.util.List;

public class BookServiceImpl implements BookService {
    // service(业务层) -> mapper层
    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

    public int deleteBook(int id) {
        return bookMapper.deleteBook(id);
    }

    public int updateBook(int id) {
        return bookMapper.updateBook(id);
    }

    public int selectBook(int id) {
        return bookMapper.selectBook(id);
    }

    public List getBooksList() {
        return bookMapper.getBooksList();
    }
}

三、整合spring

 1、创建spring-mapper.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    bean id="userService" class="com.wt.service.UserServiceImpl"/>
    bean id="testLog" class="com.wt.diy.TestLog"/>

    aop:aspectj-autoproxy />

beans>

 

SpringMVC 整合

标签:root   import   public   bsp   sql   char   hot   inter   end   

原文地址:https://www.cnblogs.com/wt7018/p/13344145.html


评论


亲,登录后才可以留言!