mybatis深入理解

准备工作:

导入依赖pom文件中

  

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Mybits</artifactId>
<groupId>com.whz</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>Mybatis-02</artifactId>
<dependencies>
<!-- mybatis必备-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--免写get set 使用注解@data即可,注意需要安装好Lombok插件才有效-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--处理DataSource需要用到-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>

<!--整合mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!-- properties的识别需要用到context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>


<!-- 配置资源过滤,以防止除了resource的xml识别不了,以及每次编译resource下的配置文件更新-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

</project>

  

  

 

Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类

 

 

可使用 SqlSessionFactoryBean来创建 SqlSessionFactory

《spring配置文件中》

配置文件类型

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>

注解类型:

@Configuration
public class MyBatisConfig {
  @Bean
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource());
    return factoryBean.getObject();
  }
}

所需的datasource:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 配置连接池属性 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/web?characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>

  



映射器类
必须是一个接口,而不是具体的实现类

假设接口名《mapper的接口》:

配置文件类型:
public interface UserMapper {
  @Select("SELECT * FROM website") 
  List<User> selectUser();

}

 注解类型: 

<?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.whz.Dao.userMapper">
    <select id="selectUser" resultType="com.whz.pojo">
   select * from website
  </select>
</mapper>

 Spring 一起使用 MyBatis使用MapperFactoryBean去整合(2)映射器(1)SqlSessionFactory 

《spring配置文件中》加入:

配置文件类型:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

注解类型:

@Configuration
public class MyBatisConfig {
  @Bean
  public UserMapper userMapper() throws Exception {
    SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
    return sqlSessionTemplate.getMapper(UserMapper.class);
  }
}

 pojo实体类:

 

package com.whz.pojo;
import lombok.Data;

@Data
public class User {
    private String  account;
    private String password;
    private String name;


}

  

 测试
public class test {
    @Test
    public void test01() {
        ApplicationContext app= new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper userMapper = (UserMapper) app.getBean("userMapper");
        List<User> users = userMapper.selectUser();
        for (User user: users){
            System.out.println(user.getName());
        }
    }
}

  



posted @ 2022-03-10 21:34  小魏同学呀  阅读(46)  评论(0)    收藏  举报