mybatis-java1234一
一,简单增删改代码实现
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一的更多相关文章
- java1234初学maven
第一讲: maven maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. maven安装与下载: .确定jdk已经安装并且配置 .安装mav ...
- mybatis, spring, springmvc
mybatis配置: mybatis-config.xml <configuration> <!-- 作者MyBatis博客: http://legend2011.blog.51ct ...
- spring springmvc mybatis 整合
环境 apache-tomcat-8.0.33.jdk1.8.0_05 maven Dynamic Web Module 2.5 1.各个xml配置文件的配置 (1)pom.xml 配置清单文件 连接 ...
- MyBatis学习笔记1--初识MyBatis
我也是初学者,写博客只是想把自己的整个思路整理一下,有不对或者不好的地方,请大家多多指正. 1.MyBatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...
- MyBatis学习笔记3--使用XML配置SQL映射器
<resultMap type="Student" id="StudentResult"> <id property="id&quo ...
- MyBatis学习笔记2--配置环境详解
1.MyBatis-config.xml详解 一个完整的配置文件如下所示 <configuration> <!-- <properties resource="jdb ...
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
- Java MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
随机推荐
- Make github as your personal maven repository
前言: 开始用maven管理java项目后,突然发现自己写了一些通用的项目想要被别的项目依赖是件很麻烦的事.公司里项目依赖可以直接有maven仓库,但个人项目呢? github 再次显示其威力了,ex ...
- python递归实现折半查找
1.Python 基础教程版:(有点没想清楚) def search(sequence, number, lower=0, upper=None): if upper is None: upper = ...
- mysql数据库迁移
由于yum安装mysql的时候,数据库的data目录默认是在/var/lib下,出于数据安全性的考虑需要把它挪到/data分区.步骤如下: 一.关闭apache和mysql. 复制代码 代码如下: s ...
- Linux下搭建Windows KMS服务器
这几天微软发布了Windows 10 RedStone 1 Build 14390, 于是我第一时间下载进行了试用.和之前那种不激活也没有任何异样不同,现在的版本如果不激活有些功能就受限了,比如你无法 ...
- ArithUtil工具类 : 精确计算各种运算
package com.autoserve.mh.common.util; import java.math.BigDecimal; import java.text.DecimalFormat; ...
- vpn
https://itunes.apple.com/us/app/sonicwall-mobile-connect/id822514576?mt=12
- 总结-javascript-ajax
<html><head><meta charset="utf-8"><script type="text/javascript& ...
- C# String 前面不足位数补零的方法
int i=10;方法1:Console.WriteLine(i.ToString("D5"));方法2:Console.WriteLine(i.ToString().PadLef ...
- python学习道路(day3note)(元组,字典 ,集合,字符编码,文件操作)
1.元组()元组跟列表一样,但是不能增删改,能查.元组又叫只读列表2个方法 一个 count 一个 index2.字典{}字典是通过key来寻找value因为这里功能比较多,所以写入了一个Code里面 ...
- [转]推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler
在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...