MyBatis学习

1.添加坐标

<packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.13version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.12version>
        dependency>
    dependencies>

2.在resources文件夹中创建(mybatis-config.xml)核心文件



<configuration>
    
    <environments default="mysql">
        
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            dataSource>
        environment>
    environments>
    
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    mappers>
configuration>

MyBatis学习_第1张图片

3.配置需要映射的xml文件以及日志文件

MyBatis学习_第2张图片






<mapper namespace="com.mybatis.dao.UserDao">
    
    <select id="findall" resultType="com.mybatis.User">
        select * from user
    select>
mapper>

日志

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

4.测试类

package test;


import com.mybatis.User;
import com.mybatis.dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class MybatisTest {
    public static void main(String[] args) throws Exception{
        //1.读取配置文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

        //2.初始化mybatis,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(in);

        //3.创建session实例
        SqlSession session=sqlMapper.openSession();

        //4.使用sqlsession创建Dao接口的代理对象
        UserDao userdao = session.getMapper(UserDao.class);

        //5.使用代理对象执行方法
        List<User> user = userdao.findall();
        for (User u:
             user) {
            System.out.println(u);
        }
        //6.释放资源
        session.close();
        in.close();
    }
}

你可能感兴趣的:(JavaEE)