1.4.?MyBatis连接UXDB

1.4.1. 安装MyBatis
1.4.2. 导入mybatis的驱动包
1.4.3. 创建接口类、配置和测试类等
1.4.4. 运行结果
1.4.5. 使用分析说明

1.4.1.?安装MyBatis

下载离线包或者在线安装插件。

1.4.2.?导入mybatis的驱动包

导入官网下载的mybatis驱动包。

1.4.3.?创建接口类、配置和测试类等

右键uxdb-jdbc,New->Source Folder,Folder name输入mybatis。

创建包(右键mybatis,New->Package)config,main,mapper,model。

包下面创建对应的类(右键包名,New->Class)MybatisConfiguration,Main,StudentsMapper,Student。

创建Mybatis配置文件(右键mybatis,New->Other...->MyBatis->MyBatis Generator Configuration File,点击Next,File name中输入mybatis-config.xml和student.xml,点击Finish)。

  1. Student.java代码如下:

    package model;
    public class Student {
        private int sno;    
        private String sname;   
        private int sage;   
        private String ssex;    
        public int getSno() {
            return sno;
        }
        public void setSno(int sno) {
            this.sno = sno;
        }
        public String getSname() {
            return sname;
        }
        public void setSname(String sname) {
            this.sname = sname;
        }
        public int getSage() {
            return sage;
        }
        public void setSage(int sage) {
            this.sage = sage;
        }
        public String getSsex() {
            return ssex;
        }
        public void setSsex(String ssex) {
            this.ssex = ssex;
        }
    }

  2. StudentsMapper代码如下:

    package mapper;
    import model.Student;
    import java.util.List;
    // Maps queries in student.xml
    public interface StudentsMapper {
        public List<Student> getStudents();
    }

  3. MybatisConfiguration.java代码如下:

    package config;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import java.io.IOException;
    import java.io.Reader;
    public class MybatisConfiguration {
        private static final String MYBATIS_CONFIG_FILE = "mybatis-config.xml";
        private final SqlSessionFactory sqlSessionFactory;
        public MybatisConfiguration() throws IOException {
            try(Reader reader = Resources.getResourceAsReader(MYBATIS_CONFIG_FILE)) {
                    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            }
        }
        public SqlSessionFactory getSessionFactory() {
            return sqlSessionFactory;
        }
    }

  4. mybatis-config.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"/>
                <dataSource type="POOLED">
                    <property name="poolMaximumActiveConnections"     value="50"/>
                    <property name="poolMaximumIdleConnections"       value="10"/>
                    <property name="poolMaximumCheckoutTime"          value="20000"/>  <!-- milliseconds -->
                    <property name="driver" value="com.uxsino.uxdb.Driver"/>
                    <property name="url" value="jdbc:uxdb://192.168.1.82:5432/test"/>
                    <property name="username" value="uxdb"/>
                    <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="student.xml"/>
        </mappers>
    </configuration>

  5. student.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="mapper.StudentsMapper">
        <select id="getStudents" parameterType="int" resultType="model.Student">
            INSERT INTO student VALUES(9999,'mybatis',99,'boy');
            SELECT * FROM student
        </select>
    </mapper>

  6. 测试代码Main.java如下:

    package main;
    import config.MybatisConfiguration;
    import mapper.StudentsMapper;
    import model.Student;
    import org.apache.ibatis.session.SqlSession;
    import java.io.IOException;
    import java.util.List;
    public class Main {
        public static void main(String[] args) throws IOException {
            // Parse the configuration file mybatis-config.xml
            MybatisConfiguration config = new MybatisConfiguration();
            List<Student> students;
            // Get a session. Get the mapper (defined in countries-languages.xml) and query using business-logic style. Close session
            SqlSession sqlSession = config.getSessionFactory().openSession();
            try {
                StudentsMapper mapper = sqlSession.getMapper(StudentsMapper.class);
                students = mapper.getStudents();
            } finally {
                if(null != sqlSession) {
                    sqlSession.close();
                }
            }
            if(students != null) {
                // Print query results
                System.out.println("Sno,Sname,Sage,Ssex");
                for(Student cl : students) {
                    System.out.println(cl.getSno() + "," + cl.getSname() + "," + cl.getSage()+ "," + cl.getSsex());
                }
            }
    }
    }

1.4.4.?运行结果

1.4.5.?使用分析说明

配置mybatis-config.xml文件注意以下几点:

  1. driverClassName="com.uxsino.uxdb.Driver" (使用uxdb的driver.class)

  2. url="jdbc:uxdb://192.168.1.82:5432/test" (jdbc:uxdb://IP:port/databasename)

  3. username="uxdb"

  4. password="123456"

XML 地图 | Sitemap 地图