mybatis 连接数据库 增删改查

数据库版本:MySQL8.0.19
目录结构:
mybatis 连接数据库 增删改查_第1张图片
pom.xml文件:


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>mybatis_day2artifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.19version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.4version>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                      <groupId>org.apache.maven.pluginsgroupId>
                      <artifactId>maven-compiler-pluginartifactId>
                      <configuration>
                        <target>1.8target>
                        <source>1.8source>
                        <encoding>UTF-8encoding>
                      configuration>
            plugin>
        plugins>
    build>
project>

domain->address.java文件和dao->IAddressDao.java接口、AddressDao.java实体类

address.java
package domain;
import java.io.Serializable;
public class Address implements Serializable {
    private Integer id;
    private String des;

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", des='" + des + '\'' +
                '}';
    }

    public Integer getId() {return id;}

    public void setId(Integer id) {this.id = id;}

    public String getDes() {return des;}

    public void setDes(String des) {this.des = des;}
}

IAddressDao.java接口
package dao;
import domain.Address;
import java.util.List;
/**
 * 持久层
 */
public interface IAddressDao {
    List<Address> findAll();
}

AddressDao.java实体类
public class AddressDao implements IAddressDao {
    private SqlSessionFactory sqlSessionFactory;

    public AddressDao(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public List<Address> findAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Address> list = sqlSession.selectList("dao.IAddressDao.findAll");//参数就是能获取配置信息的key
        sqlSession.close();
        return list;
    }

    @Override
    public void AddAddress(Address address) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("dao.IAddressDao.AddAddress",address);
        sqlSession.commit();
        sqlSession.close();
    }
}

mybatis主配置文件:




<configuration>


    <typeAliases>

        <package name="domain"/>
    typeAliases>

    <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/test2?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="totoro"/>
            dataSource>
        environment>
    environments>


    <mappers >




        <package name="dao"/>
    mappers>
configuration>

IAddressDao.xml配置文件




 <mapper namespace="dao.IAddressDao">



    <insert id="AddAddress" parameterType="Address">
        insert into address(des)values(#{des});
    insert>

    <update id="updateA" parameterType="Address">
        update address set des=#{des} where id=#{id};
    update>

    <delete id="deleteA" parameterType="int">
        delete from address where id=#{id};
    delete>

    <select id="findAll" resultType="Address">
        select * from address;
    select>

    <select id="findById" parameterType="int" resultType="Address">
        select * from address where id=#{id};
    select>

    <select id="findbyname" parameterType="string" resultType="Address">
        select * from address where des like #{desc};
    select>

    <select id="countNum" resultType="int">
        select count(*) from address;
    select>
mapper>

Test测试类:


public class MybatisTest {
    @Test
    public  void findall() throws  Exception{
        //1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //3.使用工厂生产SqlSession对象
        SqlSession sqlSession=  sqlSessionFactory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        IAddressDao iAddressDao =sqlSession.getMapper(IAddressDao.class);
        //5.使用代理对象执行方法
        List<Address> list = iAddressDao.findAll();
        for (Address a:list) {
            System.out.println(a.toString());
        }
        inputStream.close();
        sqlSession.close();
    }
    @Test
    public  void all() throws  Exception{
        //1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        IAddressDao iAddressDao = new AddressDao(sqlSessionFactory);
        List<Address> list = iAddressDao.findAll();
        for (Address a:list) {
            System.out.println(a.toString());
        }
        inputStream.close();

    }
    @Test
    public  void add()throws Exception{
        Address address = new Address();
        address.setDes("japan");
        //1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //3.使用工厂生产SqlSession对象
        SqlSession sqlSession=  sqlSessionFactory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        IAddressDao iAddressDao =sqlSession.getMapper(IAddressDao.class);
        //5.使用代理对象执行方法
        iAddressDao.AddAddress(address);

        sqlSession.commit();
        inputStream.close();
        sqlSession.close();
    }
    @Test
    public  void insert()throws Exception{
        Address address = new Address();
        address.setDes("dddddd");
        //1.读取配置文件,配置文件是mybatis的全局配置文件,不是单独个配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMybatisCon.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        IAddressDao iAddressDao = new AddressDao(sqlSessionFactory);
        iAddressDao.AddAddress(address);
        inputStream.close();

    }
}

你可能感兴趣的:(mybatis初学)