ssm--MyBatis基础day01

1.MyBatis概述

1.1 ORM框架

对象关系映射(Java中的对象 对应 数据库中的表)

1.2 官网地址

mybatis

ssm--MyBatis基础day01_第1张图片

1.3 MyBatis Plus

MyBatis-Plus (baomidou.com)官网

1.4 JPA

Java 持久层API

2. MAVEN引入

2.1 配置maven

ssm--MyBatis基础day01_第2张图片

2.2 导入MyBatis

<dependency>
  <groupId>org.mybatisgroupId>
  <artifactId>mybatisartifactId>
  <version>3.5.13version>
dependency>

2.3 MySQL driver

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.32version>
dependency>

2.4 建立目录com/itheima/mapper

2.4.1 创建mapper接口和xml文件

note:因为此时为入门所以放在一块后续则不然

ssm--MyBatis基础day01_第3张图片

2.5 配置resources

2.5.1 配置db.properties

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql:/db(数据库名)
db.username=//自己的用户名
db.password=//密码

2.5.2 src/main/resocurces文件夹下创建mybatis-config.xml/mybatis-conf.xml

建议直接复制 需要注意 里面的映射关系需要修改


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties"/>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            dataSource>
        environment>
    environments>
    <mappers>
      <package name="com/itheima/mapper"/>
    mappers>
configuration>

2.5.3 日志文件log4j2

(1)导入依赖

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-coreartifactId>
    <version>2.20.0version>
dependency>

(2)配置log4j2.xml文件

note:这里我的包名为com 也就是src/main/java/com 如果是别的需要修改对应的名字下方会有标注

log4j2.xml文件


<Configuration status="WARN">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        Console>
        <File name="LogToFile" fileName="log/test.log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        File>
    Appenders>
    <Loggers>
        
        <Logger name="com" level="error">
            <AppenderRef ref="LogToConsole"/>
        Logger>
        
        <Logger name="com" level="debug" additivity="false">
        
            <AppenderRef ref="LogToConsole"/>
            <AppenderRef ref="LogToFile"/>
        Logger>
        
        <Root level="error">
            <AppenderRef ref="LogToConsole"/>
        Root>
    Loggers>
Configuration>

整体项目结构如图(log4j2.xml 为日志文件)

ssm--MyBatis基础day01_第4张图片

2.6 编写XxxMapper和XxxMapper.xml

2.6.1 映射编写sql

Xxmapper

public interface DbMapper {
String selectversion();
}
Xxmapper.xml

select version(); ### 2.6.2 注解形式 @Select("show databases ") List dbs(); 两种方式都可正常运行

2.6.3 注意事项

因为xml文件和mapper接口在java包下系统不会自动编译其下的xml文件需要pom.xml文件中添加buder

<build>
    <resources>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.xmlinclude>
            includes>
        resource>
        <resource>
            <directory>src/main/resourcesdirectory>
            <includes>
                <include>**/*.xmlinclude>
                <include>**/*.propertiesinclude>
                <include>**/*.htmlinclude>
            includes>
        resource>
    resources>
build>

因为添加之后resources文件也有可能不会自动编译建议加上

是否编译可以看一下打包后的target/classes文件夹下是否有这个xml文件

ssm--MyBatis基础day01_第5张图片

2.7 编写测试代码

在test目录下编写测试代码 这一种需要配置mybatis-conf.xml文件链接数据库

public class Demo {
    public static void main(String[] args) {


        try (InputStream is = Resources.getResourceAsStream("mybatis-conf.xml")) {
            //建立会话工厂
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();
            DbMapper mapper = sqlSession.getMapper(DbMapper.class);
            Book book = new Book();
            book.setPrice(10.0D);
            book.setNumber(15);
            book.setBname("《斗feng大陆》");
            book.setCratedate(LocalDateTime.now());



            System.out.println(mapper.add(book));
            sqlSession.commit();
            System.out.println(mapper.selectAll());
            System.out.println(mapper.selectversion());
            System.out.println(mapper.dbs());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

也可以直接在代码中链接数据库 但是大部分时间基本不用

public class Deno {
    public static void main(String[] args) {
        PooledDataSource ds = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql://localhost:3306/mybatis","root","");
        TransactionFactory ts = new JdbcTransactionFactory();
        Environment env = new Environment("dev",ts,ds);
        Configuration cfg = new Configuration(env);
        cfg.addMapper(DbMapper.class);
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession ss = sf.openSession();
        DbMapper mapper = ss.getMapper(DbMapper.class);
        System.out.println(mapper.selectversion());
        System.out.println(mapper.dbs());
        System.out.println(mapper.selectAll());

    }
}

2.8 结果

ssm--MyBatis基础day01_第6张图片

……………………………………day01…………………………………………

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