1.准备工作
1.1准备数据库
准备好数据库,这里我用的是这么一个表。
附上代码以供白嫖
create database `ssmbuild`;
use `ssmbuild`;
drop table if exists `book`;
create table `books`(
`bookID` int(10) not null auto_increment comment 'books id',
`bookname` varchar(100) not null comment '书名',
`bookCounts` int(11) not null comment 'books id',
`detail` varchar(200) not null comment '描述',
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,'从进门到进牢');
select * from books;
1.2.创建工程
创建一个空白的maven工程,添加web依赖;
1.2.1导包
<dependencies>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- 数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</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.1-b03</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.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!-- Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- springwebmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
然后需要在这里添加一下lib
1.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>
1.3.连接数据库
idea中database连接,这个不说了吧。
1.4建包
我的结构是这样的
1.5建立空白配置文件
建立两个核心配置文件,spring的配置(applicationContext.xml)和mybatis的配置(mybatis-config.xml)
mybatis配置文件框架(干净的):
<?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>
spring配置(干净的):
<?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>
2.Mybatis
2.1连接属性设置
新建一个文件保存(database.properties)
jdbc.driver=com.mysql.jdbc.Driver
#sql版本是8.0的话 需要设置时区&serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
2.2 mybatis属性设置
写好的配置文件
<?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>
<!--配置数据源交给spring去做-->
<typeAliases>
<package name="com.feng.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.feng.dao.BookMapper"/>
</mappers>
</configuration>
2.3实体类
写实体类,用于保存数据库中取到的对象,放在pojo文件夹中。
package com.feng.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private int bookID;
private String bookname;
private int bookCounts;
private String detail;
}
2.4增删改查接口
2.4.1 bookmapper
package com.feng.dao;
import com.feng.pojo.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
int addBook(Book book);
int deleteBook(@Param("bookID") int id);
int updateBook(Book book);
Book queryByID(@Param("bookID") int id);
List<Book> queryAllBooks();
}
2.4.2 实现
mapper.xml
<?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="com.feng.dao.BookMapper">
<!--配置数据源交给spring-->
<insert id="addBook" parameterType="Book">
insert into ssmbuild.books(bookname,bookCounts,detail)
values
(#{bookname},#{bookCounts},#{detail});
</insert>
<delete id="deleteBook" parameterType="int">
delete from ssmbuild.book where bookID=#{bookID};
</delete>
<update id="updateBook" parameterType="Book">
update ssmbuild.books
set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID=#{bookID};
</update>
<select id="queryByID" parameterType="Book">
select * from ssmbuild.books where bookID=#{bookID};
</select>
<select id="queryAllBooks" parameterType="Book">
select * from ssmbuild.books;
</select>
</mapper>
2.4.3 Service 层
接口
package com.feng.service;
import com.feng.pojo.Book;
import java.util.List;
public interface BookService {
int addBook(Book book);
int deleteBook( int id);
int updateBook(Book book);
Book queryByID(int id);
List<Book> queryAllBooks();
}
实现类
package com.feng.service;
import com.feng.dao.BookMapper;
import com.feng.pojo.Book;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
//@Service
public class BookServiceImpl implements BookService{
//这里是业务层 需要调用dao层
// @Autowired
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; }
public int addBook(Book book) { return bookMapper.addBook(book); }
public int deleteBook(int id) { return bookMapper.deleteBook(id); }
public int updateBook(Book book) { return bookMapper.updateBook(book); }
public Book queryByID(int id) { return bookMapper.queryByID(id); }
public List<Book> queryAllBooks() { return bookMapper.queryAllBooks(); }
}
3 Spring层
写配置文件spring-dao.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 2.连接池 有dbcp半自动化操作不能自动连接,c3p0自动化操作(自动化加载配置文件,并且可以自动设置到对象中),hikari,druid-->
<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}"/>
<!-- c3p0连接池的私有属性-->
<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>
<!-- 3.sqlSessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置dao接口扫描包,动态实现dao接口可以注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlsessionfactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的dao包-->
<property name="basePackage" value="com.feng.dao"/>
</bean>
</beans>
spring-service.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service下的包-->
<context:component-scan base-package="com.feng.service"/>
<!-- 将我们的所有的业务类注入到spring,通过配置或者注解实现-->
<bean id="bookServiceImpl" class="com.feng.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!-- 声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="datasource"/>
</bean>
<!-- aop事务支持-->
</beans>
4 Springmvc层
4.2 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- dispatchservlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterencoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterencoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
spring mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service下的包-->
<!-- 注解驱动-->
<mvc:annotation-driven/>
<!-- 静态资源过滤-->
<mvc:default-servlet-handler/>
<!-- 扫描包 controller-->
<context:component-scan base-package="com.feng.controller"/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5 查询书籍功能
BookController.java