mybatis 简单项目步骤
mybatis.xml
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="project" />
<property name="password" value="1234" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/oracle/mapper/StudentMapper.xml" />
</mappers>
</configuration>
获取数据源
public class MyBatisSqlSession {
// 获取数据源
private static String resource = "mybatis.xml";
private static InputStream inputStream = null;
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
mapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace配置的是dao接口-->
<mapper namespace="com.oracle.dao.IStudentMapperDao">
<!--#{}固定写法,中间的值是属性值
id 属性值就是接口里面的方法名-->
<insert id="saveStudent" parameterType="com.oracle.pojo.Student">
<!-- keycolum表的字段,keyproperty属性 order=before在insert语句执行之前执行-->
<selectKey keyColumn="id" keyProperty="id" resultType="java.lang.Long" order="BEFORE">
select student_seq.nextval as id from dual
</selectKey>
insert into student (id, name, address, gender,age) values (
#{id}, #{name}, #{address}, #{gender},#{age})
</insert> <select id="getStudentById" parameterType="java.lang.Long" resultType="java.util.HashMap">
select * from student where id=#{id}
</select> <resultMap type="com.oracle.pojo.Student" id="studentResultMap">
<id column="id" property="id" javaType="long" jdbcType="BIGINT" />
<result column="name" property="name" javaType="string" jdbcType="VARCHAR"/>
<result column="address" property="address" javaType="string" jdbcType="VARCHAR"/>
<result column="gender" property="gender" javaType="string" jdbcType="VARCHAR"/>
<result column="age" property="age" javaType="int" jdbcType="INTEGER"/>
</resultMap> <select id="getStudent" parameterType="java.lang.Long" resultMap="studentResultMap">
select * from student where id=#{id}
</select> <select id="getStudentByParam22222" parameterType="java.util.HashMap" resultMap="studentResultMap">
select * from student where id=#{id} and address=#{address}
</select> <sql id="studentsql"> id,name ,address,gender,age </sql>
<sql id="wheresql"> address=#{address} </sql> <select id="getStudentByParam" parameterType="java.lang.String" resultType="java.util.HashMap">
select <include refid="studentsql"></include> from student where <include refid="wheresql"></include>
</select> <delete id="deleteStudent" parameterType="long">
delete from student where id=#{id}
</delete> <update id="updateStudent" parameterType="java.util.HashMap" >
update student set address=#{address} where id=#{id}
</update> <select id="selectByCondition" parameterType="com.oracle.pojo.Student" resultType="com.oracle.pojo.Student" >
select id,name,address,gender,age
from student
where 1=1
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
<if test="address != null">
and address like #{address}
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="age != 0">
and age = #{age}
</if>
</select> <sql id="key">
<trim suffixOverrides=",">
id,
<if test="name !=null">
name,
</if>
<if test="address !=null">
address,
</if>
<if test="gender != null">
gender,
</if>
<if test="age != 0">
age,
</if>
</trim>
</sql>
<sql id="values">
<trim suffixOverrides=",">
#{id},
<if test="name !=null">
#{name},
</if>
<if test="address !=null">
#{address},
</if>
<if test="gender != null">
#{gender},
</if>
<if test="age != 0">
#{age},
</if>
</trim>
</sql>
<insert id="dynainsert" parameterType="com.oracle.pojo.Student" >
<selectKey keyColumn="id" keyProperty="id" resultType="java.lang.Long" order="BEFORE">
select student_seq.nextval as id from dual
</selectKey>
insert into student(<include refid="key"></include>) values (<include refid="values"></include>)
</insert> <delete id="dynaDeleteArray" >
delete student where id in
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete> <delete id="dynaDeleteList">
delete from students where students_id in
<foreach collection="list" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete> <update id="dynaUpdate" parameterType="com.oracle.pojo.Student">
update student
<set>
<if test="address !=null">
address = #{address},
</if>
<if test="age!=0">
age = #{age},
</if>
</set>
where id=#{id}
</update> </mapper>
dao
public interface StudentDao {
public int save(Student stu);
public List query();
}
test
public class Student_Test {
public static void main(String[] args) {
SqlSession session = MyBatisSqlSession.getSession();
StudentDao mapper = session.getMapper(StudentDao.class);
// List list = mapper.query();
// System.out.println(list.size());
Student stu=new Student();
stu.setName("qqqqqqq");
stu.setAge(100);
int save = mapper.save(stu);
session.commit();
}
}
实体类忽略,
必须的jar包,mabatis.jar,ojdbc.jar,和一个log4j用来输出日志
log4j配置文件名:/MyWeb7.12/src/log4j.properties
log4j.rootLogger = debug , stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d %p [%c] - %m%n log4j.logger.com.ibatis=debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug,stdout
mybatis 简单项目步骤的更多相关文章
- mybatis简单项目
1,mybatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- Spring Boot Mybatis简单使用
Spring Boot Mybatis简单使用 步骤说明 build.gradle:依赖添加 application.properties:配置添加 代码编写 测试 build.gradle:依赖添加 ...
- springboot + mybatis 的项目,实现简单的CRUD
以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD 上图是项目的 ...
- IntelliJ IDEA 创建 Maven简单项目
创建简单Maven项目 使用IDEA提供的Maven工具,根据artifact创建简单Maven项目.根据下图操作,创建Maven项目. 使用IDEA提供的Maven工具创建的Maven简单项目目录结 ...
- 05_ssm基础(一)之mybatis简单使用
01.mybatis使用引导与准备 1.ssm框架 指: sping+springMVC+mybatis 2.学习mybatis前准备web标准项目结构 model中的Ticket代码如下: pack ...
- 最详细的SSM(Spring+Spring MVC+MyBatis)项目搭建
速览 使用Spring+Spring MVC+MyBatis搭建项目 开发工具IDEA(Ecplise步骤类似,代码完全一样) 项目类型Maven工程 数据库MySQL8.0 数据库连接池:Druid ...
- eclipse建立springMVC 简单项目
http://jinnianshilongnian.iteye.com/blog/1594806 如何通过eclipse建立springMVC的简单项目,现在简单介绍一下. 工具/原料 eclip ...
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)
这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...
- asp.net mvc 简单项目框架的搭建(二)—— Spring.Net在Mvc中的简单应用
摘要:上篇写了如何搭建一个简单项目框架的上部分,讲了关于Dal和Bll之间解耦的相关知识,这篇来把后i面的部分说一说. 上篇讲到DbSession,现在接着往下讲. 首先,还是把一些类似的操作完善一下 ...
随机推荐
- js 获取json对象的Key、value
<script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...
- appium之toast处理
注意 toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量. toast定位 1.先看下toast长什么样,如下图,像这种 ...
- MHA 高可用集群搭建(二)
MHA 高可用集群搭建安装scp远程控制http://www.cnblogs.com/kevingrace/p/5662839.html yum install openssh-clients mys ...
- BZOJ 2244 [SDOI2011]拦截导弹 ——CDQ分治
三维偏序,直接CDQ硬上. 正反两次CDQ统计结尾的方案数,最后统计即可. #include <cstdio> #include <cstring> #include < ...
- 【bzoj2115】[Wc2011] Xor【高斯消元】
题目大意:给出一个无向有权图,找出一条从1到n的路径,使得路径上权值的异或和最大,路径可以重复走 Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条 ...
- 常州模拟赛d2t3 小X的佛光
平日里最喜欢做的事就是蒸发学水.[题目描述]小 X 所在的城市 X 城是一个含有 N 个节点的无向图,同时,由于 X 国是一个发展中国家,为了节约城市建设的经费,X 国首相在建造 X 城时只建造 N ...
- BZOJ 2973 入门OJ4798 石头游戏
矩阵递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring ...
- (10) android控件-date
1.TimePicker <TimePicker android:id="@+id/timePicker4" android:layout_width="wrap_ ...
- HDOJ 5213
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5213 BC 上的题,题解很清楚,会莫对的应该不难, 对于一个询问,我们拆成四个询问,开始拆成求区间矩形 ...
- spring boot 添加mybatis,以及相关配置
首先在pom.xml文件里加入 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifa ...