一,简单增删改代码实现

package com.mybatis.mapper;

import java.util.List;

import com.mybatis.model.Student;

public interface StudentMapper {

    public int add(Student student);

    public int update(Student student);

    public int delete(Integer id);

    public Student findById(Integer id);

    public List<Student> findAll();
}
<?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.mybatis.mapper.StudentMapper"> <resultMap type="student" id="studentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <insert id="add" parameterType="student" >
insert into t_student values(null,#{name},#{age})
</insert> <update id="update" parameterType="student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update> <delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete> <select id="findById" parameterType="Integer" resultType="student">
select * from t_student where id=#{id}
</select> <select id="findAll" resultMap="studentResult">
select * from t_student
</select> </mapper>
package com.mybatis.model;

public class Student {

    private Integer id;
private String name;
private Integer age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} /* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString();
} }
package com.mybatis.testjdbc01;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger; import com.mybatis.mapper.StudentMapper;
import com.mybatis.model.Student;
import com.mybatsi.util.SessionFactoryUtil; public class TestJdbcMybatis01 { private static Logger logger = Logger.getLogger(TestJdbcMybatis01.class); public static void main(String[] args) {
SqlSession sqlSession = SessionFactoryUtil.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student=new Student("lis",11);
int result=studentMapper.add(student);
sqlSession.commit();
if(result>0){
System.out.println("添加成功!");
logger.info("添加成功");
}
}
}
package com.mybatis.testjdbc01;

import static org.junit.Assert.fail;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.mybatis.mapper.StudentMapper;
import com.mybatis.model.Student;
import com.mybatsi.util.SessionFactoryUtil; public class TestJdbcMybatis02 { private static Logger logger = Logger.getLogger(TestJdbcMybatis01.class);
private SqlSession sqlSession = null;
private StudentMapper studentMapper = null; /**
* 测试方法调用前执行
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession = SessionFactoryUtil.openSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
} /**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
} /**
* 测试方法
*/
@Test
public void test() {
logger.info("添加学生");
Student student=new Student("wang5",12);
int result=studentMapper.add(student);
sqlSession.commit();
} @Test
public void testUpdate()
{
logger.info("修改学生");
Student student=new Student(4,"wang2",13);
studentMapper.update(student);
sqlSession.commit();
} @Test
public void testDelete(){
logger.info("删除学生");
studentMapper.delete(8);
sqlSession.commit();
} @Test
public void testFindById(){
logger.info("根据id查找学生");
Student student = studentMapper.findById(1);
sqlSession.commit();
} @Test
public void findAll(){
logger.info("查找所有学生");
List<Student> student = studentMapper.findAll();
for(Student stu : student){
System.out.println(stu.getName());
}
sqlSession.commit();
}
}
package com.mybatsi.util;

import java.io.InputStream;

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 SessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSessionFactiory(){
if(sqlSessionFactory == null){
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
return sqlSessionFactory;
} public static SqlSession openSession(){
return getSessionFactiory().openSession();
}
}
log4j.rootLogger=info,appender1,appender2

log4j.appender.appender1=org.apache.log4j.ConsoleAppender 

log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=D:/logFile.txt log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
<?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>
<properties resource="jdbc.properties"/>
<typeAliases>
<typeAlias alias="student" type="com.mybatis.model.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/mapper/StudentMapper.xml" />
<!-- <mapper class="com.mybatis.mapper.StudentMapper"/> -->
</mappers>
</configuration>

mybatis-java1234一的更多相关文章

  1. java1234初学maven

    第一讲: maven maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. maven安装与下载: .确定jdk已经安装并且配置 .安装mav ...

  2. mybatis, spring, springmvc

    mybatis配置: mybatis-config.xml <configuration> <!-- 作者MyBatis博客: http://legend2011.blog.51ct ...

  3. spring springmvc mybatis 整合

    环境 apache-tomcat-8.0.33.jdk1.8.0_05 maven Dynamic Web Module 2.5 1.各个xml配置文件的配置 (1)pom.xml 配置清单文件 连接 ...

  4. MyBatis学习笔记1--初识MyBatis

    我也是初学者,写博客只是想把自己的整个思路整理一下,有不对或者不好的地方,请大家多多指正. 1.MyBatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...

  5. MyBatis学习笔记3--使用XML配置SQL映射器

    <resultMap type="Student" id="StudentResult"> <id property="id&quo ...

  6. MyBatis学习笔记2--配置环境详解

    1.MyBatis-config.xml详解 一个完整的配置文件如下所示 <configuration> <!-- <properties resource="jdb ...

  7. 【分享】标准springMVC+mybatis项目maven搭建最精简教程

    文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...

  8. Java MyBatis 插入数据库返回主键

    最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...

  9. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  10. 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程

    本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...

随机推荐

  1. tornado 学习笔记6 Application 源码分析

    Application 是Tornado重要的模块之一,主要是配置访问路由表及其他应用参数的设置. 源代码位于虚拟运行环境文件夹下(我的是env),具体位置为env > lib>sit-p ...

  2. tornado 学习笔记5 构建Tornado网站应用

    一个Tornado 网站应用通常由一个或多个RequestHanlde的子类.一个负责将请求路由至handlers的Application以及一个启动服务器的main()函数等组成. 一个最小的“he ...

  3. 解决Windows Server2008R2中导入Excel不能使用Jet 4.0

    操作系统使用了Windows Server2008R2(Windows Server2008R2是64位系统,同时配置IIS7.0 原读取Excel文件代码为: string strConn = st ...

  4. Redis安装部署

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据 可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(dif ...

  5. android studio 问题1

    在使用androidStudio中,经常导入其他的项目,有时候会出现以下错误: rror:FAILURE: Build failed with an exception. * What went wr ...

  6. Http状态码笔记

    1,503 服务器不可用. HTTP Error 503错误的解释:web服务器不能处理HTTP请求,可能是临时超载或者是服务器进行维护.这意味着你需要忍耐一下,等待服务器的临时处理.在这种状态下,一 ...

  7. c++ 离散数学 群的相关判断及求解

    采用C/C++/其它语言编程,构造一个n阶群<G={a,b,c,…},*>,G的阶|G|满足:3<=|G|<=6 1.判断该群是否是循环群,若是,输出该群的某个生成元. 2.给 ...

  8. hibernate 异常:could not execute statement

    错误信息: JDBC exception on Hibernate data access: SQLException for SQL [n/a]; SQL state [72000]; error ...

  9. C fgetc

    格式:int fgetc(FILE *stream); 这个函数的返回值,是返回所读取的一个字节.如果读到文件末尾或者读取出错时返回EOF. 位于stdio.h中.从流中读取字符,即从stream所指 ...

  10. linux的sysctl基本配置

    # Controls the use of TCP syncookiesnet.ipv4.tcp_syncookies = 1 # me write paramnet.ipv4.tcp_timesta ...