Spring整合Mybatis

1. 基于xml方式整合mybatis

1.1 导入相关jar包

 <dependencies>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.47version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.5.5version>
    dependency>
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>5.2.0version>
    dependency>
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.2.4version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.3.20version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>5.3.20version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>5.3.8version>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>2.0.5version>
    dependency>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.13version>
      <scope>testscope>
    dependency>
      <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-testartifactId>
          <version>5.3.20version>
      dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.18.16version>
    dependency>

    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.2.11version>
    dependency>
    <dependency>
      <groupId>org.logback-extensionsgroupId>
      <artifactId>logback-ext-springartifactId>
      <version>0.1.5version>
    dependency>
    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>jcl-over-slf4jartifactId>
      <version>1.7.25version>
    dependency>
  dependencies>

1.2 编写application配置文件加载mybatis配置


    <context:component-scan base-package="cn.zxy"/>
    
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="DataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.by.pojo"/>
        <property name="MapperLocations" value="classpath*:com/by/mapper/*Mapper.xml"/>
    bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="BasePackage" value="com.by.mapper"/>
    bean>

1.3 编写mapper接口

public interface UserMapper {
    
   public List<User> findAll();
   
   @Select("select * from T_user where id=#{id}")
   public User findById(Integer id);
   
    public void update(User user);}

1.4 编写service接口和实现类

public interface UserService {public List<User> findAll();public User findById(Integer id);
    public void update(User user);
}

实现类

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;@Override
    public List<User> findAll() {
        List<User> userList = userMapper.findAll();
        return userList;
    }@Override
    public User findById(Integer id) {return userMapper.findById(id);
    }@Override
    public void update(User user) {
        userMapper.update(user);
    }
}

测试运行

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class SpringMTest {
    @Autowired
    private UserService userService;@Test
    public void SMxmltest(){
        List<User> all = userService.findAll();
        for (User user : all) {
            System.out.println(user);
        }
    }
    @Test
    public void smfindById(){
        User byId = userService.findById(3);
        System.out.println(byId);
    }

2. 基于纯注解整合方案

2.1 编写mybatisconfig配置类

public class MybatisConfig {//定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean sfb=new SqlSessionFactoryBean();
        sfb.setTypeAliasesPackage("com.by.pojo");
        sfb.setDataSource(dataSource);
        return sfb;
    }//定义bean,返回MapperScannerConfigurer对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mscf=new MapperScannerConfigurer();
        mscf.setBasePackage("com.by.mapper");
        return mscf;
    }
}

2.2 import引入到springconfig中

@Import({jdbcConfig.class,MybatisConfig.class})

你可能感兴趣的:(Mybatis持久层框架技术,spring,mybatis,sqlserver)