Ibatis 3 終於GA了,網上找了個最簡單的樣本

來源:互聯網
上載者:User

資料來源:http://blog.sina.com.cn/s/blog_6145ed810100e570.html

 

ibatis官方首頁:http://ibatis.apache.org/

 

MySQL資料庫(庫名:test):

-- ---------------------------- -- Table structure for student -- ---------------------------- CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

先看一下工程結構:

 

代碼如下:

Student.java

package com.ibatis.bean; public class Student { private int id; private String name; private String gender; private int age; private int gradeId; public Student() { super(); } public Student(int id, String name, String gender, int age, int gradeId) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; this.gradeId = gradeId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGradeId() { return gradeId; } public void setGradeId(int gradeId) { this.gradeId = gradeId; } }

 

StudentDaoImpl.java

package com.ibatis.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.ibatis.bean.Student; import com.ibatis.dao.StudentDao; import com.ibatis.factory.IbatisSessionFactory; public class StudentDaoImpl implements StudentDao { @SuppressWarnings("unchecked") public List<Student> list() { SqlSession session = IbatisSessionFactory.getSqlSession(); List<Student> list = session.selectList("student.selectStudentList"); session.close(); return list; } public Student getStudent(Integer id) { SqlSession session = IbatisSessionFactory.getSqlSession(); Student stu = (Student) session.selectOne("student.selectStudent", id); session.close(); return stu; } }

 

studentSqlMapper.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- <mapper namespace="student"> <resultMap type="student" id="studentMapper"> <result column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> </resultMap> <select id="selectStudentList" resultType="student"> select * from student </select> <select id="selectStudent" parameterType="int" resultMap="studentMapper"> select * from student where id = #{id} </select> </mapper> --> <mapper namespace="com.ibatis.dao.StudentDao"></mapper>

 

StudentDao.java

package com.ibatis.dao; import java.util.List; import org.apache.ibatis.annotations.Select; import com.ibatis.bean.Student; public interface StudentDao { @Select("select * from student") List<Student> list(); @Select("select * from student where id = #{id}") Student getStudent(Integer id); }

 

IbatisSessionFactory.java

package com.ibatis.factory; import java.io.IOException; 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; public class IbatisSessionFactory { private static SqlSessionFactory sqlMapper; private static SqlSession sqlSession; static { try { String resource = "dao-configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlMapper = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession() { sqlSession = sqlMapper.openSession(); return sqlSession; } public static void closeSqlSession() { if (sqlSession != null) { sqlSession.close(); } } }

 

dao-configuration.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.ibatis.bean.Student" alias="student" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/ibatis/dao/sql/studentSqlMapper.xml" /> </mappers> </configuration>

 

AnnocationTest.java

package com.ibatis; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.ibatis.bean.Student; import com.ibatis.dao.StudentDao; import com.ibatis.factory.IbatisSessionFactory; public class AnnocationTest { public static void main(String[] args) { SqlSession session = IbatisSessionFactory.getSqlSession(); StudentDao dao = session.getMapper(StudentDao.class); List<Student> list = dao.list(); System.out.println(list.size()); Student stu = dao.getStudent(2); System.out.println(stu.getName()); } }

 

Test.java

package com.ibatis; import com.ibatis.bean.Student; import com.ibatis.dao.StudentDao; import com.ibatis.dao.impl.StudentDaoImpl; public class Test { public static void main(String[] args) { StudentDao dao = new StudentDaoImpl(); Student stu = dao.getStudent(2); System.out.println(stu.getName()); } }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.