SSM:Spring整合Mybatis框架

一、搭建和测试MyBatis的环境

1. 在web项目中编写mybatis-config.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 resource="db.properties" />
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <!-- 数据库驱动 -->
                <property name="driver" value="${jdbc.driver}"></property>
                <!-- 连接数据库 -->
                <property name="url" value="${jdbc.url}"></property>
                <!-- 连接数据库的用户名 -->
                <property name="username" value="${jdbc.username}"></property>
                <!-- 连接数据库的密码 -->
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--该包下所有的dao接口都可以使用-->
        <package name="cn.itcast.dao"/>
    </mappers>
</configuration>
View Code

2.在AccountDao接口的方法上添加注解,编写SQL语句

3.编写测试的方法

public class Demo1 {
    @Test
    public void run1() throws Exception{
        // 加载配置文件
        InputStream inputStream=
                Resources.getResourceAsStream("mybatis-config.xml");
        //创建工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        // 创建sqlSession对象
        SqlSession session = factory.openSession();
        //获取代理对象
        AccountDao dao=session.getMapper(AccountDao.class);
        //调用查询方法
        List<Account> list= dao.findAll();
        for(Account account:list){
            System.out.println(account);
        }
        //释放资源
        session.close();
        inputStream.close();
    }
}

二、Spring整合Mybatis框架

1. 目的:把Mybatis-config.xml配置文件中的内容配置到applicationContext.xml配置文件中

 <!--Spring整合MyBatis框架-->
    <!--配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--配置SqlSessionFactory工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--配置AccountDao接口所在包-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.itcast.dao"/>
    </bean>
View Code

注意:表现层调用业务处理层,业务处理层调用dao层;

2. AccountDao接口中添加@Repository注解

3. service中注入dao对象,进行测试

在表现层的代码如下:

 @Autowired
    private AccountService accountService;
    /*
    * 查询所有的数据
    * @return
    * */
    @RequestMapping("findAll")
    public String findAll(){
        System.out.println("表现层:查询所有账户");
        List<Account> list= accountService.findAll();
        for(Account account:list){
            System.out.println(account);
        }
        return "list";
    }

2. Spring整合MyBatis框架1. 目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中2. AccountDao接口中添加@Repository注解

一纸高中万里风,寒窗读破华堂空。 莫道长安花看尽,由来枝叶几相同?
原文地址:https://www.cnblogs.com/byczyz/p/14492045.html