MyBatis 基本配置和使用

如何使⽤

  • 新建 Maven ⼯程,pom.xml
>
 >
 >org.mybatis>
 >mybatis>
 >3.4.5>
 >
 >
 >mysql>
 >mysql-connector-java>
 >8.0.11>
 >
 >
 >org.projectlombok>
 >lombok>
 >1.18.6>
 >provided>
 >
> >
 >
 >
 >src/main/java>
 >
 >**/*.xml>
 >
 >
 >
>
  • 新建数据表
use mybatis;
create table t_account(
 id int primary key auto_increment,
 username varchar(11),
 password varchar(11),
 age int
)
  • 新建数据表对应的实体类 Account
import lombok.Data;
@Data
public class Account {
 private long id;
 private String username;
 private String password;
 private int age; }
  • 第一种:创建 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">
>
 <!-- 配置MyBatis运⾏环境 -->
 
 
 
 
 
 
 
>
 :mysql://localhost:3306/mybatis?
useUnicode=true&;characterEncoding=UTF-8">>
 >>
 >>
 >
 >
 >
>
  • 第二种:创建 MyBatis 的配置⽂件 在application.yml中(简单)
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=UTF-8&useSSL=false&&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  #  目录地址
  config-location: classpath:mybatis/mybatis-config.xml
  #  目录地址
  mapper-locations: classpath:mybatis/mapper/*.xml
  • 创建mapper.xml 写Sql语句
MyBatis 框架需要开发者⾃定义 SQL 语句,写在 Mapper.xml ⽂件中,实际开发中,会为每个实体
类创建对应的 Mapper.xml ,定义管理该对象数据的 SQL。
<?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">
>
 >
 insert into t_account(username,password,age) values(#{username},#
{password},#{age})
 >
>

<!--    namespace 通常设置为⽂件所在包+⽂件名的形式。-->
<!--    insert 标签表示执⾏添加操作。-->
<!--    select 标签表示执⾏查询操作。-->
<!--    update 标签表示执⾏更新操作。-->
<!--    delete 标签表示执⾏删除操作。-->
<!--    id 是实际调⽤ MyBatis ⽅法时需要⽤到的参数。-->
<!--    parameterType 是调⽤对应⽅法时参数的数据类型。-->

  • 第一种注册方式在全局配置⽂件 config.xml 中注册 AccountMapper.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>
 <!-- 配置MyBatis运⾏环境 -->
 <environments default="development">
 <environment id="development">
 <!-- 配置JDBC事务管理 -->
 <transactionManager type="JDBC"></transactionManager>
 <!-- POOLED配置JDBC数据源连接池 -->
 <dataSource type="POOLED">
 <property name="driver" value="com.mysql.cj.jdbc.Driver">
</property>
 <property name="url"
value="jdbc:mysql://localhost:3306/mybatis?
useUnicode=true&amp;characterEncoding=UTF-8"></property>
 <property name="username" value="root"></property>
 <property name="password" value="root"></property>
 </dataSource>
 </environment>
 </environments>
  <!-- 注册AccountMapper.xml -->
 <mappers>
 <mapper resource="com/southwind/mapper/AccountMapper.xml"></mapper>
 </mappers>
</configuration>
  • 第二种注册方式在全局配置⽂件 在application.yml 中
  #  目录地址
  config-location: classpath:mybatis/mybatis-config.xml
  #  目录地址
  mapper-locations: classpath:mybatis/mapper/*.xml
  • 调⽤ MyBatis 的原⽣接⼝执⾏添加操作。
public class Test {
 public static void main(String[] args) {
 //加载MyBatis配置⽂件
 InputStream inputStream =
Test.class.getClassLoader().getResourceAsStream("config.xml");
 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new
SqlSessionFactoryBuilder();
 SqlSessionFactory sqlSessionFactory =
sqlSessionFactoryBuilder.build(inputStream);
 SqlSession sqlSession = sqlSessionFactory.openSession();
 String statement = "com.southwind.mapper.AccoutMapper.save";
 Account account = new Account(1L,"张三","123123",22);
 sqlSession.insert(statement,account);
 sqlSession.commit();
 }
}
  • 通过 Mapper 代理实现⾃定义接⼝

1、⾃定义接⼝,定义相关业务⽅法。
2 、编写与⽅法相对应的 Mapper.xml。

import com.southwind.entity.Account;
import java.util.List;
public interface AccountRepository {
 public int save(Account account);
 public int update(Account account);
 public int deleteById(long id);
 public List<Account> findAll();
 public Account findById(long id);
}
  • 创建接⼝对应的 Mapper.xml,定义接⼝⽅法对应的 SQL 语句。

statement 标签可根据 SQL 执⾏的业务选择 insert、delete、update、select。
MyBatis 框架会根据规则⾃动创建接⼝实现类的代理对象。
规则:

  • Mapper.xml 中 namespace 为接⼝的全类名。
    Mapper.xml 中 statement 的 id 为接⼝中对应的⽅法名。
    Mapper.xml 中 statement 的 parameterType 和接⼝中对应⽅法的参数类型⼀致。
    Mapper.xml 中 statement 的 resultType 和接⼝中对应⽅法的返回值类型⼀致。
<?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">
>
 >
 insert into t_account(username,password,age) values(#{username},#
{password},#{age})
 >
 >
 update t_account set username = #{username},password = #{password},age
= #{age} where id = #{id}
 >
 >
 delete from t_account where id = #{id}
 >
 >
 select * from t_account where id = #{id}
 >
>
  • 调⽤接⼝的代理对象完成相关的业务操作
public class Test {
 public static void main(String[] args) {
 InputStream inputStream =
Test.class.getClassLoader().getResourceAsStream("config.xml");
 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new
SqlSessionFactoryBuilder();
 SqlSessionFactory sqlSessionFactory =
sqlSessionFactoryBuilder.build(inputStream);
 SqlSession sqlSession = sqlSessionFactory.openSession();
 //获取实现接⼝的代理对象
 AccountRepository accountRepository =
sqlSession.getMapper(AccountRepository.class);
 //添加对象
// Account account = new Account(3L,"王五","111111",24);
// int result = accountRepository.save(account);
// sqlSession.commit();
 //查询全部对象
// List list = accountRepository.findAll();
// for (Account account:list){
// System.out.println(account);
// }
// sqlSession.close();
 //通过id查询对象
// Account account = accountRepository.findById(3L);
// System.out.println(account);
// sqlSession.close();
 //修改对象
// Account account = accountRepository.findById(3L);
// account.setUsername("⼩明");
// account.setPassword("000");
// account.setAge(18);
// int result = accountRepository.update(account);
// sqlSession.commit();
// System.out.println(result);
// sqlSession.close();
 //通过id删除对象
 int result = accountRepository.deleteById(3L);
 System.out.println(result);
 sqlSession.commit();
 sqlSession.close();
 }
}

你可能感兴趣的:(spring)