下载离线包或者在线安装插件。
导入官网下载的mybatis驱动包。
右键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)。
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; } }
StudentsMapper代码如下:
package mapper; import model.Student; import java.util.List; // Maps queries in student.xml public interface StudentsMapper { public List<Student> getStudents(); }
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; } }
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>
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>
测试代码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()); } } } }
配置mybatis-config.xml文件注意以下几点:
driverClassName="com.uxsino.uxdb.Driver" (使用uxdb的driver.class)
url="jdbc:uxdb://192.168.1.82:5432/test" (jdbc:uxdb://IP:port/databasename)
username="uxdb"
password="123456"