当前位置: 首页 > news >正文

Spring的mybatis整合


mybatis整合

主要是处理dao包下的接口和xml文件,以及service下的类和接口

第一步

在resource目录下创建mybatis-config.xml文件【注意点:mybatis-config.xml文件下通常都是写别名、和mappers】

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><typeAliases><typeAlias type="com.pojo.Books" alias="books"/></typeAliases><mappers><mapper class="com.dao.BooksMapper"/></mappers>
</configuration>

第二步:

dao和service包下的文件

dao包

public interface BooksMapper {int add(Books books);int delete(int bookID);int update(Books books);Books selectById(int bookID);List<Books> selectAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.BooksMapper"><insert id="add" parameterType="books">insert into books(bookID,bookName,bookCount,detail) values (#{bookID},#{bookName},#{bookCount},#{detail})</insert><delete id="delete" parameterType="int">delete from books where bookID=#{bookID}</delete><update id="update" parameterType="books">update books set bookName=#{bookName},bookCount=#{bookCount},detail=#{detail} where bookID=#{bookID}</update><select id="selectById" resultType="books">select * from books where bookID=#{bookID};</select><select id="selectAll" resultType="books">select * from books</select>
</mapper>

service包

public interface BooksService {int add(Books books);int delete(int bookID);int update(Books books);Books selectById(int bookID);List<Books> selectAll();
}
public class BooksServiceImpl implements BooksService {private BooksMapper booksMapper;public void setBooksMapper(BooksMapper booksMapper) {this.booksMapper = booksMapper;}public int add(Books books) {return booksMapper.add(books);}public int delete(int bookID) {return booksMapper.delete(bookID);}public int update(Books books) {return booksMapper.update(books);}public Books selectById(int bookID) {return booksMapper.selectById(bookID);}public List<Books> selectAll() {return booksMapper.selectAll();}
}

【注意点:这里的serviceImpl别忘了给它set注入】

第三步:

最后需要在总的spring.xml文件中导入包

<import resource="classpath:spring-dao.xml"/>

spring整合

第一步:

配置整合mybatis
关联数据库文件

<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}"/><!-- 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>

配置SqlSessionFactory对象

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
注入数据库连接池
<property name="dataSource" ref="dataSource"/>

配置MyBaties全局配置文件:mybatis-config.xml

<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 3.配置SqlSessionFactory对象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 注入数据库连接池 --><property name="dataSource" ref="dataSource"/><!-- 配置MyBaties全局配置文件:mybatis-config.xml --><property name="configLocation" value="classpath:mybatis-config.xml"/></bean>

配置扫描Dao接口包,动态实现Dao接口注入到spring容器中

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 注入sqlSessionFactory --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!-- 给出需要扫描Dao接口包 --><property name="basePackage" value="com.dao"/></bean>
<?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"><!--DispatcherServlet--><servlet><servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--encodingFilter--><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>/*</url-pattern></filter-mapping><!--Session过期时间--><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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 配置SpringMVC --><!-- 1.开启SpringMVC注解驱动 --><mvc:annotation-driven/><!-- 2.静态资源默认servlet配置--><mvc:default-servlet-handler/><!-- 3.配置jsp 显示ViewResolver视图解析器 --><bean class="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><!-- 4.扫描web相关的bean --><context:component-scan base-package="com.controller" />
</beans>

http://www.lryc.cn/news/276372.html

相关文章:

  • React 入门 - 01
  • Windows Server 2019 Standard 和 Datacenter 版本差异比较
  • 计算机网络的交通灯:停止-等待协议
  • 命令行模式的rancher如何安装?
  • 苍穹外卖Day01——总结1
  • Java 基础(二)
  • BERT 模型是什么
  • Elasticsearch中object类型与nested类型以及数组之间的区别
  • 办公文档,私人专用
  • linux 使用log4cpp记录项目日志
  • Kafka集群部署
  • 软件测试|深入理解SQL CROSS JOIN:交叉连接
  • 数据权限-模型简要分析
  • echarts柱状图加单位,底部文本溢出展示
  • x-cmd pkg | gh - GitHub 官方 CLI
  • Python解析XML,简化复杂数据操作的最佳工具!
  • rpm数据库被破坏,无法使用yum
  • 国标GB28181视频监控EasyCVR平台:视频集中录制存储/云端录像功能及操作介绍
  • Wargames与bash知识11
  • Python 基础(一):基本语句
  • Hibernate实战之操作MySQL数据库(2024-1-8)
  • 【Spring Boot 3】【数据源】自定义JDBC多数据源
  • 番茄工作法
  • 【uniapp】调用阿里云OCR图片识别文字:
  • 软考高级选择考哪个好?
  • 在云服务器ECS上用Python写一个搜索引擎
  • Python在智能手机芯片研发
  • K8S学习指南(70)-K8S中的informa机制
  • 「MCU」SD NAND芯片之国产新选择优秀
  • 【QML COOK】- 002-添加一个图片