SSM配置
1、导入相关的pom依赖!
< dependencies>
< 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>
< dependency>
< groupId> com.mchange</ groupId>
< artifactId> c3p0</ artifactId>
< version> 0.9.5.2</ version>
</ dependency>
< 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>
< 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>
< 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>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.8.13</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.16.10</ version>
</ dependency>
</ dependencies>
2、Mybatis层编写
1、数据库配置文件 database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
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>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< typeAliases>
< package name = " com.lnn.pojo" />
</ typeAliases>
< mappers>
< mapper resource = " com/lnn/dao/BookMapper.xml" />
</ mappers>
</ configuration>
3、编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;
<?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.lnn.dao.BookMapper" >
< insert id = " addBook" parameterType = " Books" >
insert into books(bookName,bookCounts,detail)
values (#{bookName},#{bookCounts},#{detail});
</ insert>
< delete id = " deleteBookById" parameterType = " int" >
delete from books where bookID=#{bookId}
</ delete>
< update id = " updateBook" parameterType = " Books" >
update books
set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID=#{bookID};
</ update>
< select id = " queryBookById" resultType = " Books" >
select * from books where bookID=#{bookId};
</ select>
< select id = " queryAllBook" resultType = " Books" >
select * from books;
</ select>
< select id = " queryBookByName" resultType = " Books" >
select * from books where bookName=#{bookName};
</ select>
</ mapper>
3、Spring层
1、我们去编写Spring整合Mybatis的相关的配置文件;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" >
< context: property-placeholder location = " classpath:database.properties" />
< 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}" />
< 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>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:mybatis-config.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " sqlSessionFactoryBeanName" value = " sqlSessionFactory" />
< property name = " basePackage" value = " com.lnn.dao" />
</ bean>
</ beans>
3、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
http://www.springframework.org/schema/context/spring-context.xsd" >
< context: component-scan base-package = " com.lnn.service" />
< bean id = " BookServiceImpl" class = " com.kuang.service.BookServiceImpl" >
< property name = " bookMapper" ref = " bookMapper" />
</ bean>
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
< property name = " dataSource" ref = " dataSource" />
</ bean>
</ beans>
4、SpringMVC层
1、web.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"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: tx= " http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: component-scan base-package = " com.lnn.service" />
< bean id = " BookServiceImpl" class = " com.lnn.service.BookServiceImpl" >
< property name = " bookMapper" ref = " bookMapper" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *" propagation = " REQUIRED" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " txPointcut" expression = " execution(* com.lnn.dao.*.*(..))" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " txPointcut" />
</ aop: config>
</ beans>
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: context= " http://www.springframework.org/schema/context"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< mvc: annotation-driven>
< mvc: message-converters>
< bean class = " org.springframework.http.converter.StringHttpMessageConverter" >
< constructor-arg value = " UTF-8" />
</ bean>
< bean class = " org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
< property name = " objectMapper" >
< bean class = " org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" >
< property name = " failOnEmptyBeans" value = " false" />
</ bean>
</ property>
</ bean>
</ mvc: message-converters>
</ mvc: annotation-driven>
< mvc: default-servlet-handler/>
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"value="org.springframework.web.servlet.view.JstlView" />
< property name = " prefix" value = " /WEB-INF/jsp/" />
< property name = " suffix" value = " .jsp" />
</ bean>
< context: component-scan base-package = " com.lnn.controller" />
</ 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 resource = " spring-service.xml" />
< import resource = " spring-mvc.xml" />
</ beans>