前言 在 Maven
项目里是不能直接使用 <dependency>
导入的,需要手动添加到 Maven 本地仓库,我们在下载安装 Oracle 数据库的时候是自带了 ojdbc 的 jar 包的,我的 11g 版本 jar 在 E:\oracle\jimulu\product\11.2.0\dbhome_1\jdbc\lib
利用 Maven 命令将 jar 包添加到本地仓库:
1 mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=12.1.0.1.0 -Dpackaging=jar -Dfile=E:\oracle\jimulu\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar
解释一下上面命令:
-DgroupId、-DartifactId、-Dversion 就是 dependency
的 groupId、artifactId、version 标签,不过 version 是当前所下载数据库的版本号 -Dfile 就是我们 ojdbc 的目录,就是将该 jar 包加入 Maven 本地仓库 然后直接在 Maven 项目的 pom.xml 内配置依赖即可,标签内容要与所执行 maven 命令一致。
1 2 3 4 5 6 <dependency > <groupId > com.oracle</groupId > <artifactId > ojdbc6</artifactId > <version > 12.1.0.1.0</version > </dependency >
正式开始 好,进入正题,首先在 Oracle 中创建 User 表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 create table T_USERS ( ID NUMBER not null , NAME VARCHAR2(30 ), SEX VARCHAR2(3 ), BIRS DATE , MESSAGE CLOB ); create sequence SEQ_T_USERS_ID minvalue 1 maxvalue 99999999 start with 1 increment by 1 cache 20 ;
创建 User 实体类
1 2 3 4 5 6 7 8 9 10 @Data @NoArgsConstructor @AllArgsConstructor public class User { private Integer id; private String name; private String sex; private Date birs; private String message; }
创建 UsersMapper 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 public interface UserMapper { void add (User user) ; void update (User user) ; void delete (@Param("id") Integer id) ; User queryById (@Param("id") Integer id) ; int queryByCount (User user) ; List<User> queryByList (User user) ; }
创建配置文件( mybatis-config.xml
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?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 type ="com.zjw.oracle.entity.User" alias ="User" /> </typeAliases > <environments default ="development" > <environment id ="development" > <transactionManager type ="jdbc" /> <dataSource type ="POOLED" > <property name ="driver" value ="oracle.jdbc.OracleDriver" /> <property name ="url" value ="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name ="username" value ="scott" /> <property name ="password" value ="tiger" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="UserMapper.xml" /> </mappers > </configuration >
UserMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 <?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 ="com.zjw.oracle.mapper.UserMapper" > <resultMap type ="com.zjw.oracle.entity.User" id ="BaseResultMap" > <id property ="id" column ="id" /> <result property ="name" column ="name" /> <result property ="sex" column ="sex" /> <result property ="birs" column ="birs" jdbcType ="TIMESTAMP" /> <result property ="message" column ="message" /> </resultMap > <sql id ="Tabel_Name" > t_users </sql > <sql id ="Base_Column_List" > id, name, sex, birs, message </sql > <sql id ="Example_Where_Clause" > where 1 = 1 <trim suffixOverrides ="," > <if test ="id != null" > and id = #{id} </if > <if test ="name != null and name != ''" > and name like concat(concat('%', '${name}'), '%') </if > <if test ="sex != null and sex != ''" > and sex like concat(concat('%', '${sex}'), '%') </if > <if test ="birs != null" > and birs = #{birs} </if > <if test ="message != null" > and message = #{message} </if > </trim > </sql > <insert id ="add" parameterType ="User" > <selectKey resultType ="int" order ="BEFORE" keyProperty ="id" > select seq_t_users_id.nextval as id from dual </selectKey > insert into t_users(id, name, sex, birs, message) values(#{id}, #{name}, #{sex}, #{birs}, #{message}) </insert > <update id ="update" parameterType ="User" > update t_users set name = #{name}, sex = #{sex}, birs = #{birs}, message = #{message} where id = #{id} </update > <delete id ="delete" > delete from t_users where id = #{id} </delete > <select id ="queryById" resultMap ="BaseResultMap" > select <include refid ="Base_Column_List" /> from t_users where id = #{id} </select > <select id ="queryByCount" resultType ="java.lang.Integer" parameterType ="User" > select count(1) from t_users <include refid ="Example_Where_Clause" /> </select > <select id ="queryByList" resultMap ="BaseResultMap" parameterType ="User" > select <include refid ="Base_Column_List" /> from t_users <include refid ="Example_Where_Clause" /> </select > </mapper >
在 main 方法中测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class OracleTest { private static final String RESOURCE = "mybatis-config.xml" ; public static void main (String[] args) { SqlSession sqlSession = getSqlSessionFactory().openSession(true ); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); for (User user : userMapper.queryByList(new User ())) { System.out.println(user); } } private static SqlSessionFactory getSqlSessionFactory () { SqlSessionFactory sessionFactory = null ; try { sessionFactory = new SqlSessionFactoryBuilder ().build(Resources.getResourceAsReader(RESOURCE)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } private static Date getData (String bir) { DateFormat df = new SimpleDateFormat ("yyyy-MM-dd" ); Date date = null ; try { date = df.parse(bir); } catch (ParseException e) { e.printStackTrace(); } return date; } }