Spring-xml(+注解)方式整合第三方的框架-mybatis

1)不需要自定义命名空间:MyBatis

Spring整合Mybatis的步骤如下:

// 原始配置
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.49</version>
</dependency>
<dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.10</version>
</dependency>
// mybatis配置文件
<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/dianping"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/springdemo/service/Mapper/userMapper.xml"/>
    </mappers>
</configuration>

// mapper配置文件
<?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.springdemo.service.Mapper.userMapper">
    <select id="getList" resultType="com.springdemo.service.pojo.userDto">
        select * from tb_user
    </select>
</mapper>

//测试
InputStream in = Resources.getResourceAsStream("MybatisConfigure.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
userMapper userMapper = sqlSession.getMapper(userMapper.class);
List<userDto> list = userMapper.getList();
for(userDto userDto:list){
     System.out.println(userDto.toString());
}

// 输出
userDto{id=1, phone='13686869696', nick_name='小鱼同学', password='', icon='/imgs/blogs/blog1.jpg'}
userDto{id=2, phone='13838411438', nick_name='可可今天不吃肉', password='', icon='/imgs/icons/kkjtbcr.jpg'}
userDto{id=4, phone='13456789011', nick_name='user_slxaxy2au9f3tanffaxr', password='', icon=''}
userDto{id=5, phone='13456789001', nick_name='可爱多', password='', icon='/imgs/icons/user5-icon.png'}

  导入Mybatis整个Spring的相关坐标;

  编写Mapper和Mapper.xml

  配置SqlSessionFactoryBean和MapperScannerConfigurer

  测试

// Spring整合Mybatis
// 主要配置SqlSessionFactory 和 MapperScannerConfigurer 生成usermapper对象交给spring管理
// 依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
// 不需要mybatis配置文件和配置扫描的mapper包,直接通过spring生成bean
  <bean id="userDao" class="com.springdemo.service.Impl.userDaoImpl">
<property name="usermapper" ref="userMapper"></property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/dianping"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.springdemo.service.Mapper"></property>
</bean>
// 测试在userdao中注入usermapper对象 操作数据库输出
public class userDaoImpl implements userDao{
private userMapper usermapper;// 类中定义依赖对象
public void setUsermapper(userMapper usermapper) {// 配置设置方式,Spring将对象作为参数传给this对象
this.usermapper = usermapper;
}
@Override
public void show(){
List<userDto> list = usermapper.getList();
for(userDto userDto:list){
System.out.println(userDto.toString());
}
}
}
// 结果

userDto{id=1, phone='13686869696', nick_name='小鱼同学', password='', icon='/imgs/blogs/blog1.jpg'}
userDto{id=2, phone='13838411438', nick_name='可可今天不吃肉', password='', icon='/imgs/icons/kkjtbcr.jpg'}
userDto{id=4, phone='13456789011', nick_name='user_slxaxy2au9f3tanffaxr', password='', icon=''}
userDto{id=5, phone='13456789001', nick_name='可爱多', password='', icon='/imgs/icons/user5-icon.png'}

 

 

2)需要引入第三方框架命名空间:Dubbo 自定义命名空间 暂时先搁置

3.利用注解方式整合mybatis:标签替换为注解,配置文件替换为配置类

public class MybatisComponent {
    @Bean("dataSource")
    public DruidDataSource DataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/dianping");
        dataSource.setUsername("root");
        dataSource.setPassword("123");
        return dataSource;
    }
    @Bean
    public SqlSessionFactoryBean SqlSessionFactoryBean(@Autowired DruidDataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
}

@Configuration  // 标注当前类是一个配置类(代替配置文件) + @Component
//<context:component-scan base-package="com.springdemo.service"></context:component-scan>
@ComponentScan(basePackages = "com.springdemo.service") //basePackages 数组
//<context:property-placeholder location="classpath:test.properties"></context:property-placeholder>
@PropertySource(value = {"classpath:test.properties"}) // key-value 配置文件 数组
@MapperScan(value = {"com.springdemo.service.Mapper"})
@Import(MybatisComponent.class) // 将MybatisComponent类收纳到SpringConfig下,不需要配置@Component注解
public class SpringConfig {

}

@Mapper
public interface userMapper {
    List<userDto> getList();
}

// 测试
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
userDao usedao = (userDao) applicationContext.getBean(userDao.class);
System.out.println(usedao);
usedao.show();

// 结果

userDto{id=1, phone='13686869696', nick_name='小鱼同学', password='', icon='/imgs/blogs/blog1.jpg'}
userDto{id=2, phone='13838411438', nick_name='可可今天不吃肉', password='', icon='/imgs/icons/kkjtbcr.jpg'}
userDto{id=4, phone='13456789011', nick_name='user_slxaxy2au9f3tanffaxr', password='', icon=''}

 

热门相关:1号婚令:早安,大总裁!   明朝败家子   奈何老公太宠我   最牛兵王   重开地狱