<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>ibatisdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-sqlmap --> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> </project>
package com.mycompany.entity; public class Student { private int sid; private String name; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "id="+sid + "name="+name; } }
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <properties resource="SqlMap.properties"/> <transactionManager type="JDBC"> <dataSource type="simple"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> </dataSource> </transactionManager> <sqlMap resource="com/mycompany/entity/Student.xml"/> </sqlMapConfig>
package com.mycompany.dao; import com.mycompany.entity.Student; public interface StudentDao { /** * 通过resultMap获取学生信息 * @param sid * @return */ public Student getStudentById(int sid); }
package com.mycompany.dao.impl; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.mycompany.dao.StudentDao; import com.mycompany.entity.Student; public class StudentDaoImpl implements StudentDao { private static SqlMapClient sqlMapClient = null; static{ try { Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 查询学生信息 */ public Student getStudentById(int sid) { Student student = null; try { student = (Student) sqlMapClient.queryForObject("getStudentById",sid); } catch (SQLException e) { e.printStackTrace(); } return student; } }
package com.mycompany.dao.impl; import com.mycompany.entity.Student; public class StudentTest { /** * @param args */ public static void main(String[] args) { StudentDaoImpl studentDaoImpl = new StudentDaoImpl(); //查询学生信息 Student student5 = studentDaoImpl.getStudentById(5); System.out.println(student5); } }
本文出自 “素颜” 博客,谢绝转载!
原文地址:http://suyanzhu.blog.51cto.com/8050189/1916265