一,项目搭建准备
1,准备数据库
2,基本环境搭建
导入相关的依赖
3,maven资源过滤
4,建立项目相关结构、包
pojo的实体类包 dao的数据库交互包 service的业务层包 controller的控制器包 mybatis的配置文件:mybatis-config.xml spring的配置文件:applicationContext.xml
二,mybatis层
1,编写数据库的配置文件database.properties
jdbc
.driver
=com
.mysql
.cj
.jdbc
.Driver
jdbc
.url
=jdbc
:mysql
://localhost
:3306/user
?serverTimezone
=GMT
%2B8
jdbc
.username
=root
jdbc
.password
=root
2,编写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>
<!--配置数据源,交给spring去做
-->
<typeAliases>
<package name
="com.star.pojo"/>
</typeAliases
>
<mappers>
<mapper
class="com.star.dao.BookMapper"></mapper
>
</mappers
>
</configuration
>
3,编写数据库对应的实体类
package com
.kuang
.pojo
;
import lombok
.AllArgsConstructor
;
import lombok
.Data
;
import lombok
.NoArgsConstructor
;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID
;
private String bookName
;
private int bookCounts
;
private String detail
;
}
4,编写dao层的mapper接口
public interface BookMapper {
int addBook(Books book
);
int deleteBookById(int id
);
int updateBook(Books books
);
Books
queryBookById(int id
);
List
<Books> queryAllBook();
}
5,编写dao层的mapper对应的配置文件
<?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.star.dao.BookMapper">
<insert id
="addBook" parameterType
="Books">
insert into ssmbuild
.books
(bookName
,bookCounts
,detail
) values
(#
{bookName
},#
{bookCounts
},#
{detail
})
</insert
>
<delete id
="deleteBookById" parameterType
="int">
delete from ssmbuild
.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" resultMap
="Books">
select
* from ssmbuild
.books
where bookID
=#
{bookId
}
</select
>
<select id
="queryAllBook" resultMap
="Books">
select
* from ssmbuild
.books
</select
>
</mapper
>
6,编写service层的接口和实现类
接口:
package com
.star
.service
;
import com
.star
.pojo
.Books
;
import java
.util
.List
;
public interface BookService {
int addBook(Books books
);
int deleteBookById(int id
);
int updateBook(Books books
);
Books
queryBookById(int id
);
List
<Books> queryAllBook();
}
实现类:
package com
.star
.service
;
import com
.star
.dao
.BookMapper
;
import com
.star
.pojo
.Books
;
import java
.util
.List
;
public class BookServiceImpl implements BookService{
private BookMapper bookMapper
;
public void setBookMapper(BookMapper bookMapper
) {
this.bookMapper
= bookMapper
;
}
public int addBook(Books books
) {
return bookMapper
.addBook(books
);
}
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();
}
}
三,spring层
1,spring整合mybatis的配置文件
<?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"></context
:property
-placeholder
>
<!--2,连接池
dbcp
: 半自动化操作,不能自动连接,
c3p0
: 自动化操作,自动化的加载配置文件,自动设置到对象中
druid
:
hikari
:
-->
<bean id
="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name
="driverClass" value
="${jdbc.driver}"></property
>
<property name
="jdbcUrl" value
="${jdbc.url}"></property
>
<property name
="user" value
="${jdbc.username}"></property
>
<property name
="password" value
="${jdbc.password}"></property
>
<!-- c3p0连接池的私有属性
-->
<property name
="maxPoolSize" value
="30"/>
<property name
="minPoolSize" value
="10"/>
<!-- 关闭连接后不自动commit
-->
<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
>
<!--绑定mybatis的配置文件
-->
<property name
="configLocation" value
="classpath:mybatis-config.xml"></property
>
</bean
>
<!--配置dao接口扫描包,动态的实现了dao接口可以注入到spring容器中
-->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory
-->
<property name
="sqlSessionFactoryBeanName" value
="sqlSessionFactory"/>
<!--要扫描的包
-->
<property name
="basePackage" value
="com.star.dao"/>
</bean
>
</beans
>
2,spring整合service的配置文件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.star.service"/>
<!--将我们的所有业务类注入到spring,可以通过配置或者注解实现
-->
<bean id
="BookServiceImpl" class="com.star.service.BookServiceImpl">
<property name
="bookMapper" ref
="bookMapper"></property
>
</bean
>
<!--声明式事务控制
-->
<bean id
="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name
="dataSource" ref
="datasource"></property
>
</bean
>
</beans
>
四,SpringMVC层
1,核心配置文件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
:spring
-mvc
.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
>encodingFilter
</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
>encodingFilter
</filter
-name
>
<url
-pattern
>
2,配置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
http
://www
.springframework
.org
/schema
/mvc
/spring
-cache
.xsd http
://www
.springframework
.org
/schema
/context https
://www
.springframework
.org
/schema
/context
/spring
-context
.xsd"
>
<!--注解驱动
-->
<mvc
:annotation
-driven
></mvc
:annotation
-driven
>
<!--静态资源过滤
-->
<mvc
:default-servlet
-handler
></mvc
:default-servlet
-handler
>
<!--扫描包:controller
-->
<context
:component
-scan base
-package="com.star.controller"></context
:component
-scan
>
<!--视图解析器
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name
="prefix" value
="/WEB-INF/jsp/"/>
<property name
="suffix" value
=".jsp"/>
</bean
>
</beans
>
3,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"
xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beans
http
://www
.springframework
.org
/schema
/beans
/spring
-beans
.xsd"
>
<import resource
="spring-dao.xml"></import>
<import resource
="spring-service.xml"></import>
<import resource
="spring-mvc.xml"></import>
</beans
>