• 三、第一个程序


    1.工程目录结构

    2.需要导入的jar包

    除了外,其他的都是mybatis的核心包,直接可以在官网下载。

    3.具体程序

    package com.bjpowernode.beans;
    
    public class Student {
        private Integer id;
        private String name;
        private int age;
        private double score;
        public Student() {
            super();
            // TODO Auto-generated constructor stub
        }
        public Student(String name, int age, double score) {
            super();
            this.name = name;
            this.age = age;
            this.score = score;
        }
        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 int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public double getScore() {
            return score;
        }
        public void setScore(double score) {
            this.score = score;
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
        }
        
    }
    Student.java
    成员变量和属性的区别?
    成员变量--实体类中private修饰的字段
    属性--实体类中get/set方法名去除get/set后的名称首字母小写
    package com.bjpowernode.dao;
    
    import com.bjpowernode.beans.Student;
    
    public interface IStudentDao {
        void insertStu(Student student);
    }
    IStudentDao.java
    package com.bjpowernode.dao;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Reader;
    
    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 com.bjpowernode.beans.Student;
    
    public class StudentDaoImpl implements IStudentDao {
    
        private SqlSession sqlsession;
    
        @Override
        public void insertStu(Student student) {
            InputStream inputStream;
            try {
                //1.加载主配置文件
                inputStream = Resources.getResourceAsStream("mybatis.xml");
                //2.创建SqlSessionFactory对象
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                //3.创建sqlsession对象
                sqlsession = sqlSessionFactory.openSession();
                //4.执行相关操作
                //此处的statement指的是映射文件中相关操作的id
                sqlsession.insert("insertStudent", student);
                sqlsession.commit();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if(sqlsession != null) {
                    sqlsession.close();
                }
            }
        }
    
    }
    StudentDaoImpl.java
    package com.bjpowernode.test;
    
    import org.junit.Before;
    import org.junit.Test;
    
    import com.bjpowernode.beans.Student;
    import com.bjpowernode.dao.IStudentDao;
    import com.bjpowernode.dao.StudentDaoImpl;
    
    public class MyTest {
        private IStudentDao dao;
        
        @Before
        public void before() {
            dao = new StudentDaoImpl();
        }
        
        @Test
        public void testInsert() {
            Student student = new Student("张三1",30,99.6);
            dao.insertStu(student);
        }
    }
    MyTest.java
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.Target=System.out
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss}] %c %L %m%n
    #log4j.appender.console.layout.ConversionPattern=[%-5p] %m%n
    
    ##define a logger
    #此处的test表示mapper的namespace的名称
    log4j.logger.test=debug,console 
    #log4j.rootLogger=debug,console
    log4j.properties
    mapper.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="test">
        <insert id="insertStudent" parameterType="com.bjpowernode.beans.Student">
            <!-- name、age、score是对应参数的属性名,此处属性名刚好和成员变量名称相同 -->
            insert into student(name,age,score) values(#{name}, #{age}, #{score})
        </insert>
    </mapper>

    xml文件中的一些小的提示技巧?

    选中指定标签,按f2会出现提示框,提示内容如下

    Content Model : (properties?, settings?, typeAliases?,
    typeHandlers?, objectFactory?, objectWrapperFactory?,
    reflectorFactory?, plugins?, environments?,
    databaseIdProvider?, mappers?)

    其中逗号表示有先后顺序,“*”表示0个或多个,“+”表示一个或多个,“?”表示0个或一个

    mybatis.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="testEn">
            <!-- 测试环境 -->
            <environment id="testEn">
                <!--事务管理器 -->
                <transactionManager type="JDBC"></transactionManager>
                <!-- mybatis自带的数据库连接池POOLED,连接池是数据源的一种 -->
                <dataSource type="POOLED">
                    <!--数据库连接池四要素  -->
                    <!-- 加载驱动,value对应着所需的驱动类名,底层用反射来实现 -->
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
                    <!-- 如果端口号用的是经典端口号3306,ip和port可以省略不写,如下形式 -->
                    <!-- <property name="url" value="jdbc:mysql:///test"/> -->
                    <property name="username" value="root"/>
                    <property name="password" value="123"/>
                </dataSource>
            </environment>
            <!-- 上线环境 -->
            <environment id="onlineEn">
                <!--事务管理器 -->
                <transactionManager type="JDBC"></transactionManager>
                <!-- mybatis自带的数据库连接池POOLED,连接池是数据源的一种 -->
                <dataSource type="POOLED">
                    <!--数据库连接池四要素  -->
                    <!-- 加载驱动,value对应着所需的驱动类名,底层用反射来实现 -->
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
                    <!-- 如果端口号用的是经典端口号3306,ip和port可以省略不写,如下形式 -->
                    <!-- <property name="url" value="jdbc:mysql:///test"/> -->
                    <property name="username" value="root"/>
                    <property name="password" value="123"/>
                </dataSource>
            </environment>
        </environments>
        <!-- 注册映射文件 -->
        <mappers>
            <mapper resource="com/bjpowernode/dao/mapper.xml"/>
        </mappers>
    </configuration>
     
     
  • 相关阅读:
    SQL SERVER使用技巧集
    WIN32串口编程
    经典FLASH收藏
    Windows下WinsockAPI研究
    数据库连接大全[转自中国站长网]
    VirtualBox自动重启之谜
    写个设置命令的VBS脚本工具。
    VB中KeyCode的取法
    实现串口编程的三种方法
    .NET的命名空间
  • 原文地址:https://www.cnblogs.com/zheaven/p/8505227.html
Copyright © 2020-2023  润新知