1.创建数据库表
数据库环境搭建
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books`(
bookID INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
bookName VARCHAR(100) NOT NULL ,
bookCounts INT(11) NOT NULL ,
detail VARCHAR(200) NOT NULL ,
KEY bookID (bookID)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO books (bookID,bookName,bookCounts,detail)VALUES
(1,'java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到坐牢');
2.搭建IDEA环境
创建普通Maven项目,添加web框架4.0
2.1添加pom.xml依赖
<!-- 依赖:junit,数据库驱动,连接池,servlet ,jsp,mybatis,mybatis-spring ,spring -->
<dependencies>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 c3p0-->
<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>
2.2静态资源导出问题
<!-- 静态资源导出问题 -->
<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>
3.IDEA连接数据库
4.构建基本结构和配置框架
4.1 mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
4.2 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
5.mybatis层的编写
database.properties文件的编写
jdbc.driver=com.mysql.jdbc.Driver
#如果使用的是MySQL8.0+,增加一个时区的配置;&serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=2001
Dao层
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置数据源,交给spring去做-->
<typeAliases>
<!-- 把pojo下的类起别名,直接是类名小写 -->
<package name="com.kuang.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.kuang.dao.BooksMapper"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BooksMapper">
<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=#{bookID};
</delete>
<update id="updateBook" parameterType="Books">
UPDATE
`ssmbuild`.`books`
SET
`bookName` =#{bookName},
`bookCounts` = #{bookCounts},
`detail` = #{detail}
WHERE `bookID` = #{bookID};
</update>
<select id="queryBookByID" resultType="Books">
select * from `ssmbuild`.`books` WHERE `bookID` = #{bookID};
</select>
<select id="findAllBooks" resultType="Books">
select * from `ssmbuild`.`books`;
</select>
</mapper>