Mybatis使用注解方式配置

简介: Mybatis使用注解方式配置

1 介绍

这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper

映射文件了。我们先围绕一些基本的CRUD来学习,再学习复杂映射多表操作。

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result 一起使用,封装多个结果集

@One:实现一对一结果集封装

@Many:实现一对多结果集封装

2 初始化工程

2.1 导包

2.2 导配置

在src或resources目录下新建jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.254.128:3306/mybatis03?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

在src或resources目录下新建log4j.properties

### 设置###
log4j.rootLogger = debug,stdout
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

在src或resources目录下新建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="jdbc.properties"></properties>
    <settings>
        <setting name="logImpl" value="log4j"/>
    </settings>
    <typeAliases>
        <!--如果类比较多,可以指定包,通过扫包的方式给包中的所有类起别名-->
        <package name="cn.oldlu.domain"/>
    </typeAliases>
    <!--数据源环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

3 使用注解增删改查

3.1 环境准备

3.1.1 建库建表

CREATE DATABASE `mybatis03`;
USE `mybatis03`;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
insert  into `student`(`id`,`name`,`age`) values (1,'张三',23),(2,'李四',24),(3,'王五',25);

3.1.2 创建Student实体类

在cn.oldlu.domain包下创建Student类

package cn.oldlu.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private Integer id;
    private String name;
    private Integer age;
}

3.1.3 创建StudentDao接口

在cn.oldlu.dao包下创建StudentDao接口

package cn.oldlu.dao;
public interface StudentDao {
}

3.1.4 配置映射关系

在mybatis-config.xml中配置映射关系,因为现在映射关系通过注解体现,所以应该扫描包含注解的接口

<mappers>
    <!--扫描dao包下的所有接口中的注解-->
   <package name="cn.oldlu.dao"/>
</mappers>

3.2 查询演示

第一步:在StudentDao接口中添加查询方法

List<Student> findAll();

第二步:在查询方法上添加@select注解

@Select("select * from student")
List<Student> findAll();

第三步:编写测试方法

@Test
public void testFindAll()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    /*5.调用代理对象的方法完成查询*/
    List<Student> students = studentDao.findAll();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println(students);
}

形参中有多个值,在SQL中怎么获取?

//@Select("select * from student where id = #{arg0} and name = #{arg1}")
//@Select("select * from student where id = #{param1} and name = #{param2}")
@Select("select * from student where id = #{ID} and name = #{NAME}")
Student findByIdAndName(@Param("ID") Integer id, @Param("NAME") String name);//使用@Param给形参起名字,在SQL中使用#{名字} 获取形参的值

3.3 新增演示

第一步:在StudentDao接口中添加新增方法

int insert(Student student);

第二步:在新增方法上添加@Insert注解

/*#{name},#{age}对应的是形参Student对象的属性的名字,表示把name,age属性的值取出来*/
@Insert("insert into student(name,age) values(#{name},#{age})")
int insert(Student student);

第三步:编写测试方法,注意默认需要手动提交事务,必须手动commit

@Test
public void testInsert()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    /*5.调用代理对象的方法完成新增*/
    Student stu = new Student(null, "李克勤", 33);
    int count = studentDao.insert(stu);
    sqlSession.commit();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println("共"+count+"行记录受到影响");
}

3.4 修改演示

第一步:在StudentDao接口中添加更新方法

int update(Student student);

第二步:在更新方法上添加@Update注解

/*#{name},#{age},#{id}对应的是形参Student对象的属性的名字,表示把name,age,id属性的值取出来*/
@Update("update student set name=#{name},age=#{age} where id=#{id}")
int update(Student student);

第三步:编写测试方法,注意默认需要手动提交事务,必须手动commit

@Test
public void testUpdate()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    /*5.调用代理对象的方法完成更新*/
    Student stu = new Student(6, "李克清", 33);
    int count = studentDao.update(stu);
    sqlSession.commit();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println("共"+count+"行记录受到影响");
}

3.5 删除演示

第一步:在StudentDao接口中添加删除方法

int delete(Integer id);

第二步:在删除方法上添加@Delte注解

/*#{id}对应的是形参Student对象的属性的名字,表示把id属性的值取出来*/
@Delete("delete from student where id = #{id}")
int delete(Integer id);

第三步:编写测试方法,注意默认需要手动提交事务,必须手动commit

@Test
public void testDelete()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    /*5.调用代理对象的方法完成删除*/
    int count = studentDao.delete(1);
    sqlSession.commit();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println("共"+count+"行记录受到影响");
}

3.6 #{}与${}的区别

以下两种写法的区别

@Insert("insert into student (name,age) values('${name}',${age})")
@Insert("insert into student (name,age) values(#{name},#{age})")

$底层使用Statement,最终会拼接出一个完整的SQL, insert into student (name,age) values(‘严朝阳’,12)


#底层使用PrepareStatement ,最终拼接的SQL带占位符insert into student (name,age) values(?,?)


PrepareStatement 效率高,防止SQL注入,书写更加方便。

4 SQL功能类

4.1 介绍

我们在使用注解开发的时候有一些不足

  • sql语句中的关键字可能不小心写错。
  • 不方便执行动态SQL语句。

那么SQL功能类是个不错的选择。

使用方式分为2步

  • 第一步:使用SQL对象生成要执行的sql语句
  • 第二步:使用@SelectProvider替换之前的@Select,使用@InsertProvider替换之前的@Insert….

4.2 环境搭建

4.2.1 创建表

CREATE TABLE `user`(
    uid INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(100) NOT NULL,
    PASSWORD VARCHAR(50) NOT NULL,
    email VARCHAR(50),
    birthday DATE 
);
INSERT INTO `user` (NAME,PASSWORD,email,birthday) VALUES("尼古拉斯赵四",'123456','nglszs@qq.com','1888-09-02');
INSERT INTO `user` (NAME,PASSWORD,email,birthday) VALUES("尼古拉斯赵五",'123456','nglszw@qq.com','1889-09-03');
INSERT INTO `user` (NAME,PASSWORD,email,birthday) VALUES("尼古拉斯赵六",'123456','nglszl@qq.com','1890-09-04');
INSERT INTO `user` (NAME,PASSWORD,email,birthday) VALUES("尼古拉斯赵七",'123456','nglszq@qq.com','1891-09-05');

4.2.2 创建实体类

package cn.oldlu.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer uid;
    private String name;
    private String password;
    private String email;
    private String birthday;//数据库中是Date类型,java中可以是Date,也可以是String类型
}

4.2.3 定义UserSQL功能类

在cn.oldlu.sql 包下新建UserSQL功能类

package cn.oldlu.sql;
import org.apache.ibatis.jdbc.SQL;
public class UserSQL {
}

4.2.4 定义UserDao接口

在cn.oldlu.dao包下新建UserDao接口

package cn.oldlu.dao;
import cn.oldlu.domain.User;
import cn.oldlu.sql.UserSQL;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.List;
public interface UserDao {
}

4.3 查询@SelectProvider

4.3.1 普通查询

1.在UserSQL功能类中添加生成SQL语句的方法

/**功能:生成查询全部的SQL*/
public String findAll(){
    return new SQL(){
        {
            SELECT("*");
            FROM("`user`");
        }
    }.toString();
}

2.在UserDao接口中添加findAll方法

@SelectProvider(type = UserSQL.class, method = "findAll")
List<User> findAll();

3.测试方法

@Test
public void testFindAll()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    /*5.调用代理对象的方法完成查询*/
    List<User> users = userDao.findAll();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println(users);
}

4.3.2 动态SQL查询

1.在UserSQL功能类中添加生成SQL语句的方法

/**功能:根据传入的条件动态拼接查询SQL*/
/*#{}表示获取形参User对象的属性值,所以#{}中写的是属性名*/
public String findByCondition(User user){
    return new SQL(){
        {
            SELECT("*");
            FROM("`user`");
            if(user!=null){
                if (user.getUid() != null){
                    WHERE("uid = #{uid}");
                }
                if (user.getName() != null){
                    WHERE("name = #{name}");
                }
                if (user.getPassword() != null){
                    WHERE("password = #{password}");
                }
                if (user.getEmail() != null){
                    WHERE("email = #{email}");
                }
                if (user.getBirthday() != null){
                    WHERE("birthday = #{birthday}");
                }
            }
        }
    }.toString();
}

2.在UserDao接口中添加findAll方法

@SelectProvider(type = UserSQL.class,method = "findByCondition")
List<User> findByCondition(User user);

3.测试方法

@Test
public void testFindByCondition()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    /*5.调用代理对象的方法完成查询*/
    User user = new User();
    user.setUid(1);
    List<User> users = userDao.findByCondition(user);
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println(users);
}

4.4 新增@InsertProvider

1.在UserSQL功能类中添加生成SQL语句的方法

/**功能:插入*/
/*#{}表示获取形参User对象的属性值,所以#{}中写的是属性名*/
public String insert(User user){
    return new SQL(){
        {
            INSERT_INTO("`user`");
            VALUES("name,password,email,birthday","#{name},#{password},#{email},#{birthday}");
        }
    }.toString();
}

2.在UserDao接口中添加insert方法

@InsertProvider(type = UserSQL.class,method = "insert")
int insert(User user);

3.测试方法

@Test
public void testInsert()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    /*5.调用代理对象的方法完成查询*/
    User user = new User();
    user.setName("猪悟能");
    user.setPassword("123456");
    user.setBirthday("1800-09-09");
    user.setEmail("zhuwuneng@qq.com");
    int count =  userDao.insert(user);
    /*6.提交事物*/
    sqlSession.commit();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println("共"+count+"行受到影响");
}

4.5 更新@UpdateProvider

1.在UserSQL功能类中添加生成SQL语句的方法

/**生成根据ID更新的SQL*/
/*#{}表示获取形参User对象的属性值,所以#{}中写的是属性名*/
public String update(User user) {
    return new SQL() {
        {
            UPDATE("`user`");
            if (user.getName() != null) {
                SET("name = #{name}");
            }
            if (user.getPassword() != null) {
                SET("password = #{password}");
            }
            if (user.getEmail() != null) {
                SET("email = #{email}");
            }
            if (user.getBirthday() != null) {
                SET("birthday = #{birthday}");
            }
            WHERE("uid=#{uid}");
        }
    }.toString();
}

2.在UserDao接口中添加update方法

@UpdateProvider(type = UserSQL.class,method = "update")
int update(User user);

3.测试方法

@Test
public void testUpdate()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    /*5.调用代理对象的方法完成查询*/
    User user = new User();
    user.setUid(1);
    user.setName("猪悟能");
    user.setEmail("zhuwuneng@qq.com");
    int count =  userDao.update(user);
    /*6.提交事物*/
    sqlSession.commit();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println("共"+count+"行受到影响");
}

4.6 删除@DeleteProvider

1.在UserSQL功能类中添加生成SQL语句的方法

/**生成根据uid删除的SQL语句*/
/*#{uid}表示获取形参Integer的值,名字任意*/
public String deleteByUid(Integer uid){
    return new SQL(){
        {
            DELETE_FROM("`user`");
            WHERE("uid = #{uid}");
        }
    }.toString();
}

2.在UserDao接口中添加deleteByUid方法

@DeleteProvider(type = UserSQL.class, method = "deleteByUid")
int deleteByUid(Integer uid);

3.测试方法

@Test
public void testDelete()throws Exception{
    /*1.读取配置文件*/
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    /*2.解析配置文件*/
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    /*3.获取操作数据库的对象*/
    SqlSession sqlSession = sqlSessionFactory.openSession();
    /*4.获取代理对象*/
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    /*5.调用代理对象的方法完成查询*/
    int count =  userDao.deleteByUid(1);
    /*6.提交事物*/
    sqlSession.commit();
    /*6.释放资源*/
    sqlSession.close();
    //测试查询结果
    System.out.println("共"+count+"行受到影响");
}

4.7 常见错误

Error querying database. Cause:

org.apache.ibatis.binding.BindingException: Parameter ‘pageIndex’ not

found. Available parameters are [arg1, arg0, param1, param2] Cause:

org.apache.ibatis.binding.BindingException: Parameter ‘pageIndex’ not

found. Available parameters are [arg1, arg0, param1, param2]

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
at com.sun.proxy.$Proxy2.selectByPage(Unknown Source)
at com.mycom.myapp.courseselectionsystem.mapper.CourseMapperTest.testSelectByPage(CourseMapperTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.apache.ibatis.binding.BindingException: Parameter ‘pageIndex’ not found. Available parameters are [arg1, arg0, param1, param2]

at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:212)

at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45)

at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)

at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219)

at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)

at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)

… 28 more

错误代码

List selectByPage(int pageIndex, int pageSize);

原因

mybatis在Mapper接口使用注解开发时,传递参数时需要在前面添加@Param注解。


解决

在参数前面添加@Param注解。


注意,是要在dao层添加,即SSM项目的Mapper接口类中。


正确代码

List selectByPage(@Param(“pageIndex”) int pageIndex, @Param(“pageSize”) int pageSize);

格式即:@Param(参数名)

目录
相关文章
|
8月前
|
XML Java 数据库连接
MyBatis的常见配置
MyBatis 常见配置包括数据库连接、类型别名、映射器等核心模块,合理配置可提升开发效率与系统性能。主要内容涵盖核心配置文件结构、关键配置项详解及配置优先级说明。
778 4
|
Oracle 关系型数据库 Java
【YashanDB知识库】Mybatis-Plus适配崖山配置
【YashanDB知识库】Mybatis-Plus适配崖山配置
|
7月前
|
SQL XML Java
Mybatis基础使用知识(注解)
mybatis 通过 xml 或注解的方式将要执行的各种 statement 配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句。 最后 mybatis 框架执行sql 并将结果映射为java对象并返回。采用ORM(对象关系映射)思想解决了实体和数据库映射问题,对jdbc进行了封装,屏蔽了jdbc api 底层访问细节,使我们不用与jdbc api 打交道,就可以完成对数据库的持久化操作。
535 0
|
9月前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
535 18
|
Java 数据库连接 微服务
微服务——MyBatis配置——事务管理
本段内容主要介绍了事务管理的两种类型:JDBC 和 MANAGED。JDBC 类型直接利用数据源连接管理事务,依赖提交和回滚机制;而 MANAGED 类型则由容器全程管理事务生命周期,例如 JEE 应用服务器上下文,默认会关闭连接,但可根据需要设置 `closeConnection` 属性为 false 阻止关闭行为。此外,提到在使用 Spring + MyBatis 时,无需额外配置事务管理器,因为 Spring 模块自带的功能可覆盖上述配置,且这两种事务管理器类型均无需设置属性。
230 0
|
Java 数据库连接 数据库
微服务——MyBatis配置——多环境配置
在 MyBatis 中,多环境配置允许为不同数据库创建多个 SqlSessionFactory。通过传递环境参数给 SqlSessionFactoryBuilder,可指定使用哪种环境;若忽略,则加载默认环境。`environments` 元素定义环境配置,包括默认环境 ID、事务管理器和数据源类型等。每个环境需唯一标识,确保默认环境匹配其中之一。代码示例展示了如何构建工厂及配置 XML 结构。
223 0
|
缓存 Java 数据库连接
微服务——MyBatis配置——常见配置
本文介绍了 MyBatis 的常见配置及其加载顺序。属性配置优先级为:方法参数传递的属性 &gt; resource/url 属性中配置 &gt; properties 元素中指定属性。同时列举了多个关键配置项,如 `cacheEnabled`(全局缓存开关)、`lazyLoadingEnabled`(延迟加载)、`useGeneratedKeys`(使用 JDBC 自动生成主键)等,并详细说明其作用、有效值及默认值。这些配置帮助开发者优化 MyBatis 的性能与行为。
230 0
|
10月前
|
SQL XML Java
MyBatis注解的运用于条件搜索实践
通过上述的实践,我们可以看出MyBatis注解不仅能够实现条件搜索的需求,还能够提供灵活而强大的SQL构造能力,极大地简化了代码的复杂度,提高了开发效率。在实际的项目开发中,结合实际的业务需求合理选择MyBatis的配置方式(注解或XML),能够让数据持久层代码变得更加清晰和易维护。
193 11
|
存储 Java 数据库连接
Mybatisplus中的主要使用注解
3.有些注解需要配合其他配置使用。例如,@Version需要配合乐观锁插件使用,@EnumValue需要配合对应的TypeHandler使用。
546 11
|
SQL XML Java
MyBatis——选择混合模式还是全注解模式?
在MyBatis开发中,Mapper接口的实现方式有两种:全注解模式和混合模式。全注解模式直接将SQL嵌入代码,适合小规模、简单逻辑项目,优点是直观简洁,但复杂查询时代码臃肿、扩展性差。混合模式采用接口+XML配置分离的方式,适合大规模、复杂查询场景,具备更高灵活性与可维护性,但学习成本较高且调试不便。根据项目需求与团队协作情况选择合适模式至关重要。
278 4