4、使用注解完成全套增删改查

1.创建javaweb项目MyBaits_Part4_@Base

2.在项目的WebRoot下的WEB-INF下的lib包下添加如下jar文件

mybatis-3.2.3.jar

ojdbc14.jar

3.在src下的配置链接数据库的属性文件jdbc.properties

1 driver=oracle.jdbc.driver.OracleDriver
2 url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
3 username=holly
4 password=sys
jdbc.properties

4.在src下创建mybatisConfig.xml主配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
 3 <configuration>
 4 
 5   <!-- 1.引用jdbc链接数据库的属性文件 -->
 6   <properties resource="jdbc.properties"/>
 7   
 8   <!-- 2.类路径的别名 -->
 9   <typeAliases>
10     <typeAlias type="com.entity.Teacher" alias="Teacher"/>
11   </typeAliases>
12   
13   <!-- 3.事务和数据源的配置 -->
14   <environments default="development">
15     <environment id="development">
16       <transactionManager type="jdbc"/>
17       <dataSource type="POOLED">
18         <property name="driver" value="${driver}"/>
19         <property name="url" value="${url}"/>
20         <property name="username" value="${username}"/>
21         <property name="password" value="${password}"/>
22       </dataSource>
23     </environment>
24   </environments>
25   <!-- 4.注册映射文件 -->
26   <mappers>
27   <!-- 有映射文件时resource属性=映射文件的路径 -->
28   <!-- 使用注解没有映射文件时class属性=映射文件的路径 -->
29   <!-- 如果低版本dtd,使用注解,这里还是配置resouce=映射xml文件,
30   但是映射文件的mapper节点上只写一个namespace=映射接口的全路径
31   <mapper resource="com/mapper/TeacherMapper.xml"/>
32    -->
33     <!-- 如果高版本dtd,使用注解,这里如下配置,不需要写映射的xml文件"/>
34    -->
35   <mapper class="com.mapper.TeacherMapper"/>
36    
37   </mappers>
38   
39 </configuration>
mybatisConfig.xml

5.在src下的com.entity包下创建Teacher.java

 1 package com.entity;
 2 
 3 public class Teacher {
 4     private int tid;
 5     private String tname;
 6     
 7     public Teacher() {
 8     }
 9     public Teacher(int tid, String tname) {
10         this.tid = tid;
11         this.tname = tname;
12     }
13     public int getTid() {
14         return tid;
15     }
16     public void setTid(int tid) {
17         this.tid = tid;
18     }
19     public String getTname() {
20         return tname;
21     }
22     public void setTname(String tname) {
23         this.tname = tname;
24     }
25     @Override
26     public String toString() {
27         return "Teacher [tid=" + tid + ", tname=" + tname + "]";
28     }
29     
30 
31 }
Teacher.java

6.在src下的com.util包下创建MybatisUtil.java

 1 package com.util;
 2 
 3 import java.io.IOException;
 4 import java.io.Reader;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 public class MyBaitsUtil {
12     private static SqlSessionFactory sqlSessionFactory;
13     
14     static{
15         try {
16             Reader reader=Resources.getResourceAsReader("configuration.xml");
17             sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); 
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21         
22     }
23     /**
24      * 
25      * @param isCommit true表示执行完操作提交事务,false表示执行完不自动提交事务
26      * @return
27      */
28     public static SqlSession getSqlSession(boolean isCommit){
29         return sqlSessionFactory.openSession(isCommit);
30     }
31 
32 }
MybatisUtil.java

7.在src下的com.mapper包下创建TeacherMapper.java

 1 package com.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Delete;
 6 import org.apache.ibatis.annotations.Insert;
 7 import org.apache.ibatis.annotations.Select;
 8 import org.apache.ibatis.annotations.Update;
 9 
10 import com.entity.Teacher;
11 
12 public interface TeacherMapper {
13     @Insert("insert into teacher values(seq_teacher.nextval,#{tname}")
14     int saveTeacher(Teacher teacher);
15     
16     @Select("select * from teacher")
17     List<Teacher> findAll();
18     
19     @Select("select * from teacher where tid=#{tid}")
20     Teacher findById(int id);
21     
22     @Update("update teacher set tname=#{tname} where tid=#{tid}")
23     int updateTeacher(Teacher teacher);
24     
25     @Delete("delete from teacher where tid=#{tid}")
26     int deleteTeacher(int id);
27 }
TeacherMapper.java

8.在src下的com.test包下创建Test.java

 1 package com.test;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.session.SqlSession;
 6 
 7 import com.entity.Teacher;
 8 import com.mapper.TeacherMapper;
 9 import com.util.MyBaitsUtil;
10 
11 public class Test {
12 
13     /**
14      * @param args
15      */
16     public static void main(String[] args) {
17         SqlSession sqlSession=MyBaitsUtil.getSqlSession(false);
18         
19         TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);
20         List<Teacher> list=mapper.findAll();
21         for (Teacher teacher : list) {
22             System.out.println(teacher);
23         }
24         sqlSession.close();
25     
26     }
27 
28 }
Test.java

 

转载于:https://www.cnblogs.com/holly8/p/5796801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值