ssm整合
第一步dao层
与数据库相关
-
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 http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 引入外部properties配置文件--> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${db.driver}"/> <property name="jdbcUrl" value="${db.url}"/> <property name="user" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> <!-- 配置SqlSessionFactoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引入数据源--> <property name="dataSource" ref="dataSource" /> <!-- 绑定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 配置dao接口扫描包, 动态的实现了dao接口可以注入到spring容器中--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 动态注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 动态注入sqlSessionTemplate--> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>--> <!-- 要扫描的dao包--> <property name="basePackage" value="com.ml.mapper"/> </bean> </beans>
db.properties
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/ssmbuild db.username=root db.password=123456
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> <!-- <properties resource="db.properties"/>--> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="com.ml.pojo"/> </typeAliases> <!-- <environments default="development">--> <!-- <environment id="development">--> <!-- <transactionManager type="JDBC"/>--> <!-- <dataSource type="POOLED">--> <!-- <property name="driver" value="${db.driver}"/>--> <!-- <property name="url" value="${db.url}"/>--> <!-- <property name="username" value="${db.username}"/>--> <!-- <property name="password" value="${db.password}"/>--> <!-- </dataSource>--> <!-- </environment>--> <!-- </environments>--> <mappers> <mapper resource="com/ml/mapper/BookMapper.xml"/> </mappers> </configuration>
log4j.properties
# 全局日志配置 log4j.rootLogger=ERROR, stdout # MyBatis 日志配置 log4j.logger.com.ml.mapper=TRACE # 控制台输出 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
-
mapper接口
public interface BookMapper { public Integer addBook(Books book); public Integer deleteBookById(Integer id); public Integer updataBook(Books book); public Books queryBookById(Integer id); public List<Books> queryAllBook(); }
接口对应xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ml.mapper.BookMapper"> <insert id="addBook" parameterType="books"> insert into books(bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}) </insert> <delete id="deleteBookById" parameterType="integer"> delete from books where bookId = #{id} </delete> <update id="updataBook" parameterType="books"> update books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookId = #{bookId} </update> <select id="queryBookById" parameterType="integer" resultType="books"> select * from books where bookId = #{id} </select> <select id="queryAllBook" resultType="books"> select * from books </select> </mapper>
第二步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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd "> <context:component-scan base-package="com.ml.service"/> <!-- 将service层实现类注入IOC容器--> <bean id="bookServiceImpl" class="com.ml.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!-- 一个事务管理器对象--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean> <!-- 配置事务管理器对象属性--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 对所有方法生效--> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 使用aop切入--> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.ml.mapper.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
-
service层接口
public interface BookService { public Integer addBook(Books book); public Integer deleteBookById(Integer id); public Integer updataBook(Books book); public Books queryBookById(Integer id); public List<Books> queryAllBook(); }
service层实现类
public class BookServiceImpl implements BookService{ private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } public Integer addBook(Books book) { return bookMapper.addBook(book); } public Integer deleteBookById(Integer id) { return bookMapper.deleteBookById(id); } public Integer updataBook(Books book) { return bookMapper.updataBook(book); } public Books queryBookById(Integer id) { return bookMapper.queryBookById(id); } public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } }
第三步mvc层
视图控制层
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
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 开启注解扫描-->
<context:component-scan base-package="com.ml.controller"/>
<!-- 开启静态资源处理器-->
<mvc:default-servlet-handler/>
<!-- 开启注解驱动-->
<mvc:annotation-driven/>
<!-- 视图解析器-->
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
控制层
package com.ml.controller;
import com.ml.pojo.Books;
import com.ml.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @Auther: coding
* @Date: 2021/5/9 23:08
* @Description:
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
private BookService bookService;
// 查询所有书籍
@RequestMapping("/allBook")
public String allBook(Model model){
List<Books> books = bookService.queryAllBook();
model.addAttribute("allBook",books);
return "allBook";
}
// 添加书籍
@RequestMapping("/addBook")
public String addBook(Books books){
bookService.addBook(books);
return "redirect:/book/allBook";
}
// 跳转到添加书籍的页面
@RequestMapping("/toAddBook")
public String toAddBook(){
return "addBook";
}
// 跳转到修改书籍页面
@RequestMapping("/toUpdataBook/{bookId}")
public String toUpdataBook(@PathVariable("bookId") Integer bookId, Model model){
Books books = bookService.queryBookById(bookId);
model.addAttribute("book",books);
return "upDataBook";
}
// 修改书籍
@RequestMapping("/upDataBook")
public String upDataBook(Books books){
bookService.updataBook(books);
return "redirect:/book/allBook";
}
// 删除书籍
@RequestMapping("/deleteBook/{bookId}")
public String deleteBook(@PathVariable("bookId") Integer id){
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
}
第四层整合
application.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="classpath:spring-dao.xml"/>
<import resource="classpath:spring-mvc.xml"/>
<import resource="classpath:spring-service.xml"/>
</beans>
编写顺序一般为 : --> dao层 --> service层 --> mvc层