ibatis配置

 

包:相关数据库jdbc的jar包,ibatis包

=================================

 

SqlMap.properties

 

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis
username=root
password=

======================================================

 

SqlMapConfig.xml

 

<?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>
 <!-- 引用JDBC属性的配置文件 -->
 <properties resource="SqlMap.properties" />
 <!-- 使用JDBC的事务管理 -->
 <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="ibatis/app/Student.xml" />
</sqlMapConfig>

 

=======================================================

 

student.xml

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>
 <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 -->
 <typeAlias alias="Student" type="com.model.Student" />
 
 <!--
 <resultMap class="com.zd.model.Student" id="Student">
     <result property="id" column="id"/>
     <result property="name" column="name"/>
     <result property="birth" column="birth"/>
     <result property="score" column="score"/>
 </resultMap>
  -->
 

 <!-- 这样以后改了sql,就不需要去改java代码了 -->
 <!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
 <select id="selectAllStudent" resultClass="Student">
  select *
  from tbl_student
 </select>
 
 <select id="selectStudentName" resultClass="Student">
  select id, Name
  from tbl_student
 </select>

 <!-- parameterClass表示参数的内容 -->
 <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->
 <select id="selectStudentById" parameterClass="int" resultClass="Student">
  select * from tbl_student where id=#id#
 </select>

 <!-- 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject -->
 <select id="selectStudentByName" parameterClass="String"
  resultClass="Student">
  select name,birth,score from tbl_student where name like
  '%$name$%'
 </select>

 <insert id="addStudent" parameterClass="Student">
  insert into
  tbl_student(name,birth,score) values
  (#name#,#birth#,#score#);
  <selectKey resultClass="int" keyProperty="id">
   select @@identity as inserted
   <!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: -->
   <!-- mysql:SELECT LAST_INSERT_ID() AS VALUE -->
   <!-- mssql:select @@IDENTITY as value -->
   <!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL -->
   <!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。
    有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 -->
  </selectKey>
 </insert>
 
 
 <insert id="addStudent2" parameterClass="Student">
     insert into tbl_student
     (id, name, birth, score)
     values(#id#, #name#, #birth#, #score#);
   
 </insert>

 <delete id="deleteStudentById" parameterClass="int">
  <!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的name会从Student里的属性里去查找 -->
  <!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 -->
  delete from tbl_student where id=#id#
 </delete>

 <update id="updateStudent" parameterClass="Student">
  update tbl_student set
  name=#name#,birth=#birth#,score=#score# where id=#id#
 </update>

</sqlMap>

 

 

==================================


public class StudentDao implements IStudentDao {
 
 private static SqlMapClient sqlMapClient = null;

 // 读取配置文件
 static {
  try {
   Reader reader = Resources
     .getResourceAsReader("SqlMapConfig.xml");
   sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
   reader.close();
  } catch (IOException e) {
   System.out.println("读SqlMapConfig.xml 出错");
   e.printStackTrace();
  }
 }

 
 public List<Student> selectAllStudent() throws SQLException {
  List<Student> list = sqlMapClient.queryForList("selectAllStudent");
  return list;
 }
 
 public List<Student> selectStudentName() throws SQLException {
  List<Student> list = sqlMapClient.queryForList("selectStudentName");
  return list;
 }
 
 
 public boolean addStudent(Student student)throws SQLException{
  Object object = null;
  boolean flag = false;
  try {
   object = sqlMapClient.insert("addStudent", student);
   System.out.println("添加学生信息的返回值:" + object);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  if (object != null) {
   flag = true;
  }
  return flag;
 }
 
 public void save(Student stu) throws Exception{
  sqlMapClient.insert("addStudent2", stu);
 }
 
 
 public Student getStudent(int id) throws SQLException{
  Student stu = (Student) sqlMapClient.queryForObject("selectStudentById", id);
  return stu;
  
 }
 
 
 public int delete(int id) throws SQLException{
  int num = sqlMapClient.delete("deleteStudentById", id);
  return num;
 }
 
 public List findStudent(String name) throws SQLException{
  List stuList = sqlMapClient.queryForList("selectStudentByName", name);
  return stuList;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值