标签:mapper oid org color gen static 分享图片 文章 source
@参考文章,在此基础上略作修改
1,新建maven项目导入依赖
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> </dependencies>
2,com.mybatis包下新建Yanan.java
package com.mybatis; public class Yanan { private int iid; private String ifullname; private String iloginname; private String ipassword; public Yanan() { } public Yanan(int id, String name) { this.iid = id; this.ifullname = name; } public String getIfullname() { return ifullname; } public int getIid() { return iid; } public void setIid(int iid) { this.iid = iid; } public void setIfullname(String ifullname) { this.ifullname = ifullname; } public String getIloginname() { return iloginname; } public void setIloginname(String iloginname) { this.iloginname = iloginname; } public String getIpassword() { return ipassword; } public void setIpassword(String ipassword) { this.ipassword = ipassword; } }
3,根目录下新建Yanan.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="Yanan"> <select id="selectUser" parameterType="int" resultType="Yanan"> SELECT * FROM Yanan WHERE id = #{id} </select> <select id="selectUsers" resultType="Yanan"> SELECT * FROM Yanan </select> </mapper>
4,根目录下新建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> <typeAliases> <typeAlias alias="Yanan" type="com.mybatis.Yanan"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=test" /> <property name="username" value="sa" /> <property name="password" value="yanan" /> </dataSource> </environment> </environments> <mappers> <mapper resource="Yanan.xml"/> </mappers> </configuration>
5.com.controller包下新建测试文件
package com.controller; import java.io.IOException; import java.io.Reader; import java.util.List; 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.mybatis.Yanan; public class Test { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String resource = "configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); SqlSession session = ssf.openSession(); try{ Yanan user = session.selectOne("selectUser", 1); System.out.println(user.getIfullname()); System.out.println(user); System.out.println("--------------分隔线---------------"); List<Yanan> users = session.selectList("selectUsers"); for(int i=0; i<users.size(); i++) { System.out.println(users.get(i).getIfullname()); } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } }
源码包下载:JavaMybatis.rar
项目结构:
标签:mapper oid org color gen static 分享图片 文章 source
原文地址:https://www.cnblogs.com/yanan7890/p/9583622.html