... ...
- - - - - - 以下为正文 - - - - - -
MyBatis笔记
第1刷,南湖立交2021.12,课程来源:B站up/黑马
代码模板
代码模板/pom.xml
```
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!--junit 单元测试-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.3</version>
<scope>test</scope>
</dependency>
<!-- 添加slf4j日志api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!-- 添加logback-classic依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 添加logback-core依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
</dependencies>
```
代码模板/jdbc.properties
```
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db03
jdbc.username=root
jdbc.password=1111
```
代码模板/SQLMapConfig.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标签加载外部properties文件-->
<properties resource="jdbc.properties"></properties>
<!--自定义别名-->
<typeAliases>
<typeAlias type="com.it.domain.User" alias="user"></typeAlias>
<typeAlias type="com.it.domain.Order" alias="order"></typeAlias>
</typeAliases>
<!--配置分页助手插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"></property>
</plugin>
</plugins>
<!--数据源环境-->
<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>
<!--加载映射文件-->
<mappers>
<!--xml配置开发用mapper-->
<mapper resource="com/it/mapper/UserMapper.xml"></mapper>
<!--注解开发用package-->
<package namespace="com/it/mapper"/>
</mappers>
</configuration>
```
代码模板/XxMapper.xml
```
<!-- 定义与 SQL 映射文件同名的 Mapper 接口,并且将 Mapper 接口和 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">
<mapper namespace="com.it.mapper.OrderMapper">
<resultMap id="orderMap" type="order">
<id column="oid" property="id"/>
<!-- 单行数据封装方案:<result>-->
<result column="total" property="total"/>
<!-- <result column="username" property="user.username"/>-->
<!-- <result column="password" property="user.password"/>-->
<!-- 多行数据封装方案:<association>-->
<association property="user" javaType="user">
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</association>
</resultMap>
<select id="findallorderuser" parameterType="order" resultMap="orderMap">
select *,ordert.id oid from ordert,user where uid=user.id
</select>
</mapper>
```
代码模板/Xx.java
```
// //属性写在实体类里,方法写在接口里
//一对多\多对多查询的情况下,其他表的数据封装为实体类中的一个对象或以对象为元素的List集合
public class User {
public int id;
public String username;
public String password;
Order order=new Order();//单行查询返回单对象,例如findById
List<Order> orderList;//多行查询返回多对象,例如findAll
//实际需要但此处省略getter,setter,toString
}
```
代码模板/XxMapper.java
```
public interface UserMapper {
//属性写在实体类里,方法写在接口里
//数据->typeAlisas别名
//insert->save()
//select->find()
//数据表的列->字段
//实体类的变量->属性
public void save();//insert\update\delete无需返回数据,故用void
public User findById();//select返回单行数据封装为单个对象
public List<User> findall();//select返回多行数据封装为多个对象
}
```
代码模板/MyBatisDemo.java
```
//无注解入门版
private static void test1() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
OrderMapper orderMapper=sqlSession.getMapper(OrderMapper.class);
Order order=new Order();
order.setId(1);
List<Order> orderList = orderMapper.findallorderuser(order);
for (Order orders : orderList) {
System.out.println(orders);
}
sqlSession.close();
}
//注解版
public class AnnoDemo {
private UserMapper mapper;
@BeforeEach
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
void test1save()throws IOException{...}
//C,create==insert==save,需要参数,无返回值
//R,retrieve==select==find,根据给定条件查询需要参数,无条件查询不需要参数..返回单行数据封装为单个对象Xx,返回多行数据封装为多个对象List<Xx>
//U,update,需要参数,无返回值
//D,delete,需要参数,无返回值
```
代码模板/<select><if>
```
<select id="findByCondition" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</where>
</select>
```
代码模板/<sql><include>
<sql id="selectUser">select * from user</sql>
<select id="findByCondition" parameterType="user" resultType="user">
<include refid="selectUser"></include>
</select>
代码模板/findAll()
// 查询所有数据 - basic
//
interface BrandMapper{
List<Brand> selectAll();
}
//
<select id="selectAll" resultType="brand">
select * from tb_brand;
</select>select>
//
<mapper namespace="com.itheima.mapper.BrandMapper">
<select id="selectAll" resultType="brand">
select * from tb_brand;
</select>
</mapper>
//
BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
List<Brand> brands=brandMapper.selectAll();
代码模板/<resultMap>
<resultMap id="brandResultMap" type="brand">
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
应用场景:查询部分字段
只查询部分字段而不是所有字段,就可以使用resultMap的方式.
resultMap定义字段和属性的映射关系.
字段名==属性名的,不需要定义出来.
sql语句使用resultMap时,<select>标签内引入相关resultMap,如下
<select id="selectAll" resultMap="brandResultMap">
select * from tb_brand;
</select