美文网首页
原生的插入

原生的插入

作者: _FireFly_ | 来源:发表于2020-11-06 09:28 被阅读0次
MyBatis基本配置
        1.导包
        2.核心配置文件     configuration.xml  (目的 存储与数据库相关联的那些信息  driver  url  username  password)
            文件的内容可以参考MyBatis.pdf官方文档    英文版   页码7页左右地方 提供了dtd规则     
            文档中的跟标记   configuration
                两个字标记    environments
                        environment   id
                       mappers
                        mapper  resourct="另一个文件"
        3.写mapper文件(dao类的个数一致)
            存储某一个dao类中执行时需要的所有sql语句
            文件名任意
            文件的内容参考MyBatis.pdf官方文档        英文版   页码8页偏下部分
            跟标记mapper
                mapper标记中有一个属性    namespace    目前为止随意  强烈建议dao类全名
                mapper中有好多子标签
                    <insert id="">  <delete>  <update>  <select>
                    子标签的id理论上可以随意   强烈建议 id---dao中的方法名一致
        4.给我们提供了一个SqlSession对象  去做事
        5.传递一些信息    sql   问号信息    如果是查询  还需要告诉结果类型
        6.SqlSession对象默认是手动提交事务 如果想要自动   openSession(true);

configuration.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/school?useSSL=false"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/StudentMapper.xml"></mapper>
    </mappers>
</configuration>

Student

package domain;

import java.sql.Date;

public class Student {

    private Integer id;
    private String name;
    private Integer sex;
    private Integer birth;
    private Date ctime;

    public Student() {
    }

    public Student(Integer id, String name, Integer sex, Integer birth, Date ctime) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.birth = birth;
        this.ctime = ctime;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getBirth() {
        return birth;
    }

    public void setBirth(Integer birth) {
        this.birth = birth;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }
}

StudentDao

public class StudentDao {

    //设计一个方法 新增一条学生记录
    public void insert(){
        //JDBC流程----不需要我们写啦
        //MyBatis帮我们做操作

        //创建工人对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
        //获取工厂对象
        SqlSessionFactory factory = builder.build(inputStream);//图纸 核心配置文件
        //获取提供的那个对象SqlSession
        SqlSession sqlSession = factory.openSession(true);//自动开启事务 自动提交  默认false 自动开启事务  不提交
        //帮我们执行数据库操作    增删改
        sqlSession.insert("insert");//加载驱动 获取连接(连接池) 创建状态参数

        //sqlSession.commit();
    }

}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentDao">  <--namespace和dao类全名 -->
    <insert id="insert">  <--id和dao类中对应方法全名 -->
        insert into student values(10,'zzt',1,1993,'2019-10-24');
    </insert>
</mapper>

相关文章

网友评论

      本文标题:原生的插入

      本文链接:https://www.haomeiwen.com/subject/mllovktx.html