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

spring集成mybatis

1、新建一个javaEE web项目

2、加入相关依赖的坐标

<dependencies><!--数据系列:mybatis,mysgl,druid数据源,junit--><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.2</version></dependency><!-- 阿里数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>provided</scope></dependency><!--spring系列:spring-context,aop,jdbc--><!-- spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.2.RELEASE</version></dependency><!-- spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.2.RELEASE</version></dependency><!--aop--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.2.2.RELEASE</version></dependency>
</dependencies>

3、创建相应的包和类,以登录为例

package com.ffyc.ssm.model;import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;import java.util.Date;
@Data
public class Admin {private Integer id;private String account;private String password;@DateTimeFormat(pattern = "yyyy-MM-dd")private Date birthday;
}
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version><scope>provided</scope>
</dependency>

@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。

@Service(value = "loginService")
@Transactional
public class LoginService {//注入dao层对象@AutowiredLoginDao loginDao;//注入的直接是接口的代理对象sqlsession.getMapper();public Admin login(Admin admin){Admin a=loginDao.Login(admin);return a;}
}
@Repository
public interface LoginDao {public Admin Login(Admin admin);
}
<?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.ffyc.ssm.dao.LoginDao"><select id="Login" resultType="com.ffyc.ssm.model.Admin">select * from admin where account=#{account} and password=#{password}</select>
</mapper>

4、创建并配置spring和mybatis配置文件

1.mybatis配置文件(mybatis-config.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings> <!--常用设置--><!--启用日志功能,在运行时,可以将实际执行的sgL细节打印到控制台--><setting name="logImpl" value="STDOUT_LOGGING"/><setting name="mapUnderscoreToCamelCase" value="true"/><setting name="cacheEnabled" value="true"/>  <!--全局开启二级缓存--></settings><typeAliases> <!--配置类型简称--><package name="com.ffyc.ssm.model"/></typeAliases>
</configuration>

2.数据库连接配置

mybatis-config.xml

classDriverName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssmdb?serverTimezone=Asia/Shanghai
uname=root
pwd=123456

db.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--配置jdbc功能--><!--负责加载config.properties文件--><context:property-placeholder location="classpath:config.properties"></context:property-placeholder><!--spring统一管理数据库链接对象--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${classDriverName}"></property><property name="url" value="${url}"></property><property name="username" value="${uname}"></property><property name="password" value="${pwd}"></property><property name="initialSize" value="5"></property><property name="maxActive" value="10"></property></bean><!-- 配置spring事务管理类, 并注入数据源 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--开启注解事务管理--><tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

3.spring集成mybatis

Spring 集成 Mybatis 其核心是将 SqlSessionFactory 交由 Spring 管理,并由 Spring 管理对 dao 接口的代理实现。

3.1 导入 mybatis jar 包

<!--spring集成mybatis框架-->
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version>
</dependency>

3.2 配置 sqlSessionFactory,spring-mybatis.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--导入数据库连接 以及事务管理配置--><import resource="classpath:db.xml"></import> <!--target/classes下的--><!--让spring框架生成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><!--扫描SQL映射文件--><property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property></bean><!--让spring框架生成接口代理对象--><bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.ffyc.ssm.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
</beans>

4.spring.xml

spring.xml中需要导入spring-mybatis.xml,spring-mybatis.xml中导入db.xml

<!--spring.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--开启注解扫描--><context:component-scan base-package="com.ffyc.ssm"> </context:component-scan><!--开启自动代理--><aop:aspectj-autoproxy /><!--导入mybatis配置文件--><import resource="classpath:spring-mybatis.xml"></import></beans>

5、测试

public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("spring.xml");LoginService loginService=app.getBean("loginService",LoginService.class);Admin admin=new Admin();admin.setAccount("admin");admin.setPassword("admin");Admin a=loginService.login(admin);System.out.println(a);}
}

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

相关文章:

  • 抽象轻松c语言
  • Redis布隆过滤器原理
  • 写代码时候的命名规则、命名规范、命名常用词汇
  • Linux之iptables防火墙
  • 启动服务报错:Command line is too long Shorten command line for xxx or also for Spri
  • docker安装elasticsearch、kibana
  • 前端 CSS - 如何隐藏右侧的滚动条 -关于出现过多的滚动条导致界面不美观
  • 2.神经网络的实现
  • 合宙Air724UG LuatOS-Air LVGL API控件-键盘 (Keyboard)
  • pytorch深度学习实践
  • 直方图反向投影(Histogram Backprojection)
  • day32 泛型 数据结构 List
  • DW-AHB Central DMAC
  • JavaScript设计模式(四)——策略模式、代理模式、观察者模式
  • JS画布的基本使用
  • c++ set/multiset
  • 多线程与高并发——并发编程(4)
  • 设计模式之建造者模式
  • 源码编译安装opencv4.6.0,别的版本也行
  • 【MongoDB】Springboot中MongoDB简单使用
  • Python 面试:单元测试unit testing 使用pytest
  • 螺旋矩阵、旋转矩阵、矩阵Z字打印
  • Seaborn绘制热力图的子图
  • C++二级题目4
  • Tomcat 部署时 war 和 war exploded区别
  • Delphi IdTcpServer IdTcpClient 传输简单文本
  • 界面控件Telerik UI for WPF——Windows 11主题精简模式提升应用体验
  • PoseC3D 基于人体姿态的动作识别新范式
  • html2canvas 截图空白 或出现toDataURL‘ on ‘HTMLCanvasElement或img标签没截下来 的所有解决办法
  • Eclipse错误提示: Symbol ‘xxxx‘ could not be resolved