Mybatis学习(一)Mybatis基本使用及其配置

0.前提准备

配置文件mybatis-config.xml

<configuration>
  <environmenets default="development">
  <transactionManager type="JDBC"/>
  <dataSource type="POOLED">
      <property name="driver" value="com.mysql.jdbc.Driver">
     <property name="url" value="jdbc:mysql://localhost:3306/abc">
     <property name="username" value="root">
     <property name="password" value="123456">
   </dataSource>
 </environmenets >
<mappers>
   <mapper resource="com/abc/mapper/userMapper">
</mappers>
</configuration>

Mapper映射文件
如:UserMapper.xml

<!-- 
id : 唯一标识符
resultType : 返回值类型 全类名
#{id} : 从传过来的参数中取出id值
-->
<mapper namespace="com.abc.mapper.UserMapper">
<select id="selectUser" resultType="com.abc.entryUser">
  select * from User where id = #{id}
</select>
<mapper>

1.最初的方式

使用mubatis

String resource = "mybatis-config.xml";
InputStream inputStream= Resource.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().bulid(inputStream);
//2.获得SqlSession实例,能直接执行已经映射的sql语句	
SqlSession openSession = sqlSessionFactory.openSession();
try{
User user = openSession,selectOne("com.abc.UserMapper.selectOne",1);
}finally{
      openSession.close();
      }

2.改为使用接口

(1)创建接口
在com.abc.dao下创建UserMapper接口

public interface UserMapper{
 public User getUerById(Integer id);

}

(2)Mapper映射文件中的namespace指定为接口的全类名
(3) 方法id改为接口方法名

<mapper namespace="com.abc.dao.UserMapper">
<select id="getUerById" resultType="com.abc.entryUser">
  select * from User where id = #{id}
</select>
<mapper>

(4)使用mybatis

String resource = "mybatis-config.xml";
InputStream inputStream= Resource.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().bulid(inputStream);
//2.获得SqlSession实例,能直接执行已经映射的sql语句	
SqlSession openSession = sqlSessionFactory.openSession();
try{
//3.获取接口的实现类
//会为接口自动创建代理对象,代理对象去执行增删改查
UserMapper mapper = openSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
}finally{
      openSession.close();
      }

总结

1.dao接口 --> daoImpl , Mapper接口 -->mapper.xml
2.SqlSession代表和数据库的一次对话,用完必须关闭
3.SqlSession 相当于 connection , 他们都是非线程安全的
4.mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象(将接口和xml绑定)
5.两个重要的配置文件:
(1)mybatis全局配置文件:包含数据库连接信息,事务管理信息,系统运行信息…
(2)sql映射文件:保存了每个sql语句的映射信息

一些优化

1.mybatis-config.xml中使用properties文件配置数据库数据
2.settings运行时行为设置
3.typeAliases别名处理器
4.package:为某个包下的所有类批量起别名
5.类批量起别名的情况下,可以到类上使用注解@Alias(“user”)为某个类单独起一个别名
6.databaseIdProvider:支持多数据库厂商支持.
7.mappers标签,将sql注入到全局配置文件中

<!--1.mybatis-config.xml中使用properties文件配置数据库数据-->
<properties resource="dbconfig.properties"></properties>
<configuration>
  <environmenets default="development">
  <transactionManager type="JDBC"/>
  <dataSource type="POOLED">
      <property name="driver" value="${driver}">
     <property name="url" value="${url}">
     <property name="username" value="${username}">
     <property name="password" value="${password}">
   </dataSource>
 </environmenets >
 
<!--2.settings运行时行为设置-->
<settings>
    <!-- setting 用来设置每一个设置, name设置名 , value设置值-->
    <!--开启驼峰命名转换-->
    <setting name="mapUnderscoreToCamelCase" value="true">
</settings>

<!--3.typeAliases别名处理器-->
  <typeAliases>
  <!--typeAlias 为某个java类型起别名
       type:指定要起的别名的类型全类名;默认别名是类名小写(com.abc.entry.User > user)
       alias:自己指定别名
   -->
   <typeAlias type="com.abc.entry.User" >
  </typeAliases>

<!--4.package:为某个包下的所有类批量起别名,别名默认是类名小写-->
<!-- name : 指定包名-->
<package name="com.abc.entry">

<!--6.databaseIdProvider:支持多数据库厂商支持
 type="DB_VENDOR" :VendorDatabaseIdProvider
 作用就是得到数据库厂商的标识(驱动getDatabaseProductName())
 使用后到对应的mapper.xml中的语句中标识databaseId="mysql/oracle/(别名)"
-->
<databaseIdProvider type="DB_VENDOR">
     <!--为不同的数据库厂商起别名-->
   <property name="MySQl" value="mysql">
   <property name="Oracle" value="oracle">
</databaseIdProvider>

<!--7.mappers标签,将sql注入到全局配置文件中-->
<mappers>
   <!--
         一.mapper:注册一个sql映射
           resource属性:引用类路径下的sql映射文件
           url属性:引用网络路径或者磁盘路径下的Sql映射文件
           class属性:引用(注册)接口;
           如果使用注册接口,
           1.有sql映射文件,映射文件名必须和接口名相同,并且放在与接口同一目录下;
           2.没有sql映射文件,所有的sql都是利用注解写在接口上;在对应接口的方法上使用@select("select * from user where id = #{id}")或者@insert等等注解.
        二.package:批量注册,name属性,写到包名           
   -->
   <mapper resource="com/abc/mapper/userMapper">
   <package name="com.abc.dao">
</mappers>
</configuration>
<mapper namespace="com.abc.mapper.UserMapper">
<select id="selectUser" resultType="com.abc.entryUser" databaseId="mysql">
  select * from User where id = #{id}
</select>
<mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 中配置一对多关系,可以通过使用 `@TableField` 注解和自定义查询方法实现。以下是详细的配置步骤: 1. 首先,在父实体类中添加一个集合类型的字段,用于存储子实体的列表。例如,如果存在一个用户实体 User 和一个订单实体 Order,一个用户可以有多个订单,则可以在 User 实体中添加一个 List<Order> 类型的字段。 ```java public class User { // 其他字段... @TableField(exist = false) private List<Order> orders; // getter 和 setter 方法... } ``` 2. 在 User 实体类上使用 `@TableName` 注解指定数据库表名,使用 `@TableId` 注解指定主键字段。 ```java @TableName("user") public class User { @TableId("id") private Long id; // 其他字段... @TableField(exist = false) private List<Order> orders; // getter 和 setter 方法... } ``` 3. 在订单实体 Order 中,使用 `@TableField` 注解指定关联的外键字段。假设订单表中有一个外键字段 `user_id` 与 User 表的主键 `id` 关联。 ```java public class Order { @TableField("user_id") private Long userId; // 其他字段... // getter 和 setter 方法... } ``` 4. 在 UserMapper 接口中,编写自定义的查询方法,使用 MyBatis-Plus 的查询构造器来关联查询用户和订单,并将订单列表赋值给用户的 orders 字段。例如,查询某个用户及其关联的订单: ```java public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user WHERE id = #{id}") User selectUserWithOrders(@Param("id") Long id); } ``` 在上述方法中,通过 SQL 查询语句关联查询用户和订单,并使用 `@Select` 注解指定查询语句。使用 `@Param` 注解传递参数,将查询结果赋值给 User 对象。 5. 最后,在业务层调用自定义的查询方法,即可获取用户及其关联的订单信息。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserWithOrders(Long id) { return userMapper.selectUserWithOrders(id); } } ``` 通过调用 `getUserWithOrders` 方法,即可获取指定用户及其关联的订单信息。 这样就实现了 MyBatis-Plus 的一对多关联查询配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值