提示:开发环境iead, tomcat 8,maven3.6,mysql5.7
代码如下(示例):
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_topicame
,bookCounts
,detail
)VALUES
(1,‘Java’,1,‘从入门到放弃’),
(2,‘MySQL’,10,‘从删库到跑路’),
(3,‘Linux’,5,‘从进门到进牢’);```
提示:以下maven项目结构,下面案例可供参考
提示:项目结构中目录颜色正常显示表示项目新建成功
代码如下(示例):
<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>
代码如下(示例):
<!--引入maven资源过滤 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
<!--引入maven资源过滤 -->
提示:列表有显示则提示驱动导入成功,按照此方法导入所需jar包即可
提示:点击“+”进行添加
Data Sours-mysql:
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>
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 queryAllBook();
}
3.daoMapper编写:
update ssmbuild.books
set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
where bookID = #{bookID}
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 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 queryAllBook() {
return bookMapper.queryAllBook();
}
}
6.mybatis配置文件mybatis-config.xml中加载接口信息配置: