ssm框架整合—-Mybatis

  • Post author:
  • Post category:其他


提示:开发环境iead, tomcat 8,maven3.6,mysql5.7



一.基本环境搭建



1.数据库创建(表)

代码如下(示例):

REATE TABLE

books

(


bookID

INT(10) NOT NULL AUTO_INCREMENT COMMENT ‘书id’,


bookName

VARCHAR(100) NOT NULL COMMENT ‘书名’,


bookCounts

INT(11) NOT NULL COMMENT ‘数量’,


detail

VARCHAR(200) NOT NULL COMMENT ‘描述’,

KEY

bookID

(

bookID

)

) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO

books

(

bookID

,

bookN

help_topic“help_topic

ame

,

bookCounts

,

detail

)VALUES

(1,‘Java’,1,‘从入门到放弃’),

(2,‘MySQL’,10,‘从删库到跑路’),

(3,‘Linux’,5,‘从进门到进牢’);“`



2.创建简单maven项目


提示:以下maven项目结构,下面案例可供参考


在这里插入图片描述


提示:项目结构中目录颜色正常显示表示项目新建成功



1.pom.xml中y引入Junit,mysql,mybatis,mybatis-spring驱动(注意各jar包的版本)

代码如下(示例):

<dependencies>
    <!--Junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!--数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <!-- 数据库连接池 c30p0-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>

    <!--Servlet - JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!--Mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.2</version>
    </dependency>

    <!--Spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>

    </dependencies>



2maven资源过滤引入

代码如下(示例):


<!--引入maven资源过滤   -->
    <build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>

    </build>
    <!--引入maven资源过滤   -->



3.maven项目结构中查看pom.xml引入导入的驱动报

代码如下(示例):

在这里插入图片描述

提示:列表有显示则提示驱动导入成功,按照此方法导入所需jar包即可



二、IEAD手动配置mysql数据库



步骤



1 view–tool Windows–Database:

在这里插入图片描述

提示:点击“+”进行添加

Data Sours-mysql:

在这里插入图片描述



3、建立项目基本结构和配置框架:

1.项目基本结构:

com.cn.pojo

com.cn.dao

com.cn.service

com.cn.controller

2.配置框架:

2.1Mybatis配置文件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>




</configuration>

2.2Spring 配置文件application.xm:

<?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">

</beans>



4.Mytabis编写

1、数据库配置文件 database.properties:

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

 ...
 1.实体类的编写:生成set,get,tostring,有参方法,无参方法等
 package com.kuang.pojo;
public class Books {

private int bookID;
private String bookName;
private int bookCounts;
protected String detall;

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

    public Books() {
    }

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

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

    public void setName(String name) {
        bookName = name;
    }

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

    public void setDetall(String detall) {
        this.detall = detall;
    }

    public int getBookID() {
        return bookID;
    }

    public String getName() {
        return bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public String getDetall() {
        return detall;
    }
}
2.dao层接口编写:

public interface BookMapper {
    //增加一个Book
    int addBook(Books book);

    //根据id删除一个Book
    int deleteBookById(int id);

    //更新Book
    int updateBook(Books books);

    //根据id查询,返回一个Book
    Books queryBookById(int id);

    //查询全部Book,返回list集合
    List<Books> queryAllBook();
}
3.daoMapper编写:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!-- 每个mapper对应一个相应的接口,namespace对应具体的映射接口  -->
    <mapper namespace="com.kuang.dao.BookMapper">
    <!-- 每个mapper对应一个相应的接口,namespace对应具体的接口位置  -->

    <!--id表示接口的方法名 parameterType表示参数返回类型  resultType 表示查询结果返回类型 -->

    <select id="addBook" parameterType="Books" >
     <!-- 注意数据库表字段和实体类以及sql语句中名称保持一致,别名下章详解-->

       insert  into  ssmbuild.books(bookID,bookName,bookCounts,detail)
        values (#(bookID),#(bookName),#(bookCounts),
        #(detail)
        )
    </select>


    <select id="deleteBookById" parameterType="int">
     delete from ssmbuild.books where bookID=#(bookID)

    </select>


    <!--更新Book-->
    <update id="updateBook" parameterType="Books">
        update ssmbuild.books
        set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
        where bookID = #{bookID}
    </update>

    <!--根据id查询,返回一个Book-->
    <select id="queryBookById" resultType="Books">
        select * from ssmbuild.books
        where bookID = #{bookID}
    </select>

    <!--查询全部Book-->
    <select id="queryAllBook" resultType="Books">
        SELECT * from ssmbuild.books
    </select>


</mapper>
4.service接口编写(dao层=service层):
package com.kuang.service;

import com.kuang.pojo.Books;

import java.util.List;

public interface BookService {
/**
 作者
 时间
 类的描述
 ***/

//增加一个Book
int addBook(Books book);

    //根据id删除一个Book
    int deleteBookById(int id);

    //更新Book
    int updateBook(Books books);

    //根据id查询,返回一个Book
    Books queryBookById(int id);

    //查询全部Book,返回list集合
    List<Books> queryAllBook();
}

5.serviceImpl层编写:


public class BookServiceImpl implements  BookService {
    //dao和service进行组合
    private BookMapper bookMapper;

    public BookMapper getBookMapper() {
        return bookMapper;
    }

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

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

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

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

    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    public List<Books> queryAllBook() {
        return  bookMapper.queryAllBook();
    }

}
6.mybatis配置文件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.kuang.pojo"></package>
</typeAliases>
    <!--
    mapper标签绑定 映射接口地址 <mapper class="配置相对路径"/>
    <mapper url="配置绝对路劲"/>
    <mapper resource="接口信息配置"/>
     -->
<mappers >
  <!-- 使用接口信息进行配置-->
<mapper class="com.kuang.dao.BookMapper"/>
    <!-- 使用接口信息进行配置-->

</mappers>
    <!--mybatis配置文件中绑定 BookMapper接口 -->

</configuration>




版权声明:本文为m0_67929156原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。