SSM-MyBatis in Spring简单构建day02

SSM-MyBatis in Spring

1.Spring配置文件

1.0 depences Maven依赖

 <dependencies>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.32version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.13version>
        dependency>


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>6.0.8version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>6.0.8version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>3.0.1version>
        dependency>
        
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-apiartifactId>
            <version>5.9.2version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>6.0.8version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.17version>
        dependency>
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-coreartifactId>
            <version>2.20.0version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.26version>
            <scope>providedscope>
        dependency>
    dependencies>

1.1 src/main/java/com/itheima/config/Appconfig.class

package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

//配置文件标记
@Configuration
//扫描包
@ComponentScan("com.itheima")
//导入db.properties文件
@PropertySource("classpath:db.properties")
//扫描Mapper包
@MapperScan("com.itheima.mapper")
public class AppConfig {
    @Value("${db.driver}")
    String driver;
    @Value("${db.url}")
    String url ;
    @Value("${db.username}")
    String username;
    @Value("${db.password}")
    String password;
    @Bean(name = "ds",initMethod = "init",destroyMethod = "close")
    public DruidDataSource ds() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
    @Bean(name = "sf")
    public SqlSessionFactoryBean sf(DruidDataSource ds) throws IOException {
        SqlSessionFactoryBean sf = new SqlSessionFactoryBean();
        sf.setDataSource(ds);
        sf.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*Mapper*.xml"));
        return sf;
    }
    @Bean(name = "sl")
    public SqlSession sl(SqlSessionFactoryBean sf) throws Exception {
        return sf.getObject().openSession();
    }

}

1.2 src/main/resource/db.properties

db.driver=com.mysql.cj.jdbc.Driver

db.url=jdbc:mysql:/mybatis
db.username=root
db.password=

1.3 创建Mapper

SSM-MyBatis in Spring简单构建day02_第1张图片

1.4 编写测试项目test目录下

@SpringJUnitConfig(AppConfig.class)
public class TestMyBatis {
    @Autowired
    ApplicationContext ctx;
    @Autowired
    SqlSessionFactoryBean sf;
    @Autowired
    SqlSession sl;
    @Autowired
    BookMapper bookMapper;
    @Test
    void selectAll(){
        List<Map<String, Object>> dbs = bookMapper.dbs();
        System.out.println(dbs);

    }
}

2.1Mapper中的简单方法

public interface BookMapper {
    @Select("show databases")
     List<Map<String, Object>> dbs();
    @Select("select * from book")
    List<Book> selectAll();
    @Insert("insert into book(bid, bname, price, number, cratedate) values(null,#{bname},#{price},#{number},#{cratedate}) ")
    int add(Book book);
    Book selectById(int id);
}

2.2 XxxMapper.xml中的方法

<mapper namespace="com.itheima.mapper.BookMapper">

    <select id="selectById" resultType="com.itheima.pojo.Book">
        select * from book where bid = #{id}
    select>
mapper>

2.3 运行结果

浅色为日志文件
SSM-MyBatis in Spring简单构建day02_第2张图片

你可能感兴趣的:(mybatis,spring,java)