最近公司项目要使用myBatis,自己以前没有接触过,就在网上找到了一些资料研究了些。初步做出了基于myBatis连接mysql数据库的jdbc实现的功能。

employee.java

package com.org.position.model;

public class employee {
private int employeeId;// 员工id
private String employeeName; //员工姓名
private String employeeSax; //员工性别
private String employeePost; //员工职务
private String employeeDepartment; //员工所在部门 public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeSax() {
return employeeSax;
}
public void setEmployeeSax(String employeeSax) {
this.employeeSax = employeeSax;
}
public String getEmployeePost() {
return employeePost;
}
public void setEmployeePost(String employeePost) {
this.employeePost = employeePost;
}
public String getEmployeeDepartment() {
return employeeDepartment;
}
public void setEmployeeDepartment(String employeeDepartment) {
this.employeeDepartment = employeeDepartment;
} }

 employeeDao.java

package com.org.position.dao;

import java.util.List;

import com.org.position.model.employee;

public interface employeeDao {
public int countAll();
public employee findEnterprise(int employeeId);
public int insertEnterprise(employee emp);
public int deleteEnterprise(int employeeId);
public List<employee> getAllEnterprise();
public int updateEnterprise(employee emp );
public List<employee> findEnterpriseByExampleIf(employee emp); }

employeeDaoimpal.java

package com.org.position.dao.impal;

import java.io.IOException;
import java.io.Reader;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.org.position.dao.employeeDao;
import com.org.position.model.employee; public class employeeDaoimpal implements employeeDao { @Override
public int countAll() {
String resource="mybaits-config.xml";
Reader reader=null;
SqlSessionFactory ssf=null;
SqlSession session=null;
int iCount=0;
try {
reader= Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
ssf=builder.build(reader);
session=ssf.openSession();
iCount=session.selectOne("com.org.position.dao.employeeDao.countAll");
} catch (IOException e) {
e.printStackTrace();
}
finally{
session.close();
}
return iCount;
} @Override
public employee findEnterprise(int employeeId) {
String resource="mybaits-config.xml";
Reader reader=null;
SqlSessionFactory ssf=null;
SqlSession session=null;
employee emp=null;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
ssf = builder.build(reader);
session=ssf.openSession();
employeeDao empdao=session.getMapper(employeeDao.class);
emp=empdao.findEnterprise(employeeId);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
} return emp;
} @Override
public int insertEnterprise(employee emp) {
String resource="mybaits-config.xml";
Reader reader=null;
SqlSessionFactory ssf=null;
SqlSession session=null;
int count=0;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
ssf = builder.build(reader);
session=ssf.openSession();
count=session.insert("com.org.position.dao.employeeDao.insertEnterprise",emp);
session.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
session.close();
}
return count;
} @Override
public int deleteEnterprise(int employeeId) {
String resource="mybaits-config.xml";
Reader reader = null;
SqlSessionFactory ssf=null;
SqlSession session=null;
int iCount = 0;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
ssf = builder.build(reader);
session=ssf.openSession();
iCount=session.delete("com.org.position.dao.employeeDao.deleteEnterprise", employeeId);
session.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
} return iCount;
} @Override
public List<employee> getAllEnterprise() {
String resource="mybaits-config.xml";
Reader reader = null;
SqlSessionFactory ssf=null;
SqlSession session=null;
List<employee> emps = null;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
ssf = builder.build(reader);
session=ssf.openSession();
emps=session.selectList("com.org.position.dao.employeeDao.getAllEnterprise");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
session.close();
}
return emps;
} @Override
public int updateEnterprise(employee emp) {
String resource="mybaits-config.xml";
Reader reader = null;
SqlSessionFactory ssf=null;
SqlSession session=null;
int iCount = 0;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); ssf = builder.build(reader);
session=ssf.openSession();
iCount=session.delete("com.org.position.dao.employeeDao.updateEnterprise",emp);
session.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
} return iCount;
} @Override
public List<employee> findEnterpriseByExampleIf(employee emp) {
String resource="mybatis-config.xml";
Reader reader = null;
SqlSessionFactory ssf=null;
SqlSession session=null;
List<employee> emps = null;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
ssf = builder.build(reader);
session=ssf.openSession();
emps=session.selectList("com.org.position.dao.employeeDao.findEnterpriseByExampleIf",emp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
} return emps;
} }

 employeeDaoMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.org.position.dao.employeeDao">
<resultMap type="com.org.position.model.employee" id="empResultMap">
<id property="employeeId" column="employeeId"/>
<result property="employeeName" column="employeeName" />
<result property="employeeSax" column="employeeSax"/>
<result property="employeePost" column="employeePost"/>
<result property="employeeDepartment" column="employeeDepartment"/>
</resultMap>
<select id="findEnterpriseByExampleIf" parameterType="com.org.position.model.employee" resultMap="empResultMap">
select * from employee where 1=1
<if test="employeed !=null">
and employeeId=#{employeeId}
</if>
<if test="employeename!=null">
and employeeName=#{employeeName}
</if>
<if test="employeesax! =null">
and employeeSax=#{employeeSax}
</if>
<if test="employeepost!=null">
and employeePost=#{employeePost}
</if>
<if test="employeedepartment! =null">
and employeeDepartment=#{employeeDepartment}
</if>
</select> <select id="countAll" resultType="int">
select count(*) from t_employee
</select>
<!-- 根据id查询对应的数据信息 -->
<select id="findEnterprise" parameterType="int" resultType="com.org.position.model.employee">
select * from t_employee where employeeId=#{employeeId}
</select>
<!-- 查询所有 数据 -->
<select id="getAllEnterprise" resultType="com.org.position.model.employee">
select * from t_employee
</select>
<!-- 添加数据信息 -->
<insert id="insertEnterprise" parameterType="com.org.position.model.employee">
insert into t_employee(employeeId,employeeName,employeeSax,employeePost,employeeDepartment)
values (#{employeeId},#{employeeName},#{employeeSax},#{employeePost},#{employeeDepartment})
</insert>
<!-- 更新数据信息 -->
<update id="updateEnterprise" parameterType="com.org.position.model.employee">
update t_employee set employeeName=#{employeeName},employeeSax=#{employeeSax},employeePost=#{employeePost},employeeDepartment=#{employeeDepartment}
where employeeId=#{employeeId}
</update>
<!-- 删除数据信息 -->
<delete id="deleteEnterprise" parameterType="com.org.position.model.employee">
delete from t_employee where employeeId=#{employeeId} </delete>
</mapper>

 mybaits-config.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"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/org/position/dao/employeeDaoMapper.xml"/>
</mappers>
</configuration>

 employeeTest.java

package com.org.position.test;

import java.util.List;

import com.org.position.dao.employeeDao;
import com.org.position.dao.impal.employeeDaoimpal;
import com.org.position.model.employee; public class employeeTest { public static void testAddEmp(){
employee emp=new employee();
employeeDao dao=new employeeDaoimpal();
emp.setEmployeeId(11);
emp.setEmployeeName("爽");
emp.setEmployeeSax("女");
emp.setEmployeePost("测试经理");
emp.setEmployeeDepartment("研发部");
int count =dao.insertEnterprise(emp);
if (count==1) {
System.out.println("员工信息录入成功!");
}else {
System.out.println("员工信息录入失败!");
} } public static void testUpdateEmp(){
employee emp=new employee();
emp.setEmployeeId(1);
emp.setEmployeeName("雷浩明");
emp.setEmployeeSax("男");
emp.setEmployeePost("CEO");
emp.setEmployeeDepartment("总裁办");
employeeDao dao=new employeeDaoimpal();
int count =dao.updateEnterprise(emp);
if (count==1) {
System.out.println("员工信息修改成功!");
}else {
System.out.println("员工信息修改失败!");
} }
public static void testSelectEmp(){ }
public static void testDeleteEmp(){
employeeDao dao =new employeeDaoimpal();
int count = dao.deleteEnterprise(11);
if(count == 1){
System.out.println("员工信息删除成功");
}else {
System.out.println("员工信息删除失败");
}
} public static void testGetCount(){
employeeDao dao =new employeeDaoimpal();
System.out.println("共有"+dao.countAll()+"名员工");
} public static void testGetEmpById(){
employeeDao dao =new employeeDaoimpal();
employee emp=dao.findEnterprise(1);
System.out.println("职务是:"+emp.getEmployeePost()+" 所在部门:"+emp.getEmployeeDepartment());
} public static void testGetAll(){
employeeDao dao =new employeeDaoimpal();
List<employee> emps = dao.getAllEnterprise();
System.out.println("西安涯创信息技术有限公司员工明细");
System.out.println("---------------------------");
System.out.println("员工编号\t员工姓名\t员工职位\t所在部门");
for (employee emp : emps) {
System.out.println(emp.getEmployeeId()+"\t"+emp.getEmployeeName()+"\t"+emp.getEmployeePost()+"\t"+emp.getEmployeeDepartment());
}
} public static void main(String[] args) {
//testAddEmp();
//testUpdateEmp();
//testDeleteEmp();
//testGetCount();
//testGetEmpById();
testGetAll();
} }

t_employee.sql

/*
SQLyog Ultimate v8.32
MySQL - 5.0.27-community : Database - test
*********************************************************************
*/ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `test`; /*Table structure for table `t_employee` */ DROP TABLE IF EXISTS `t_employee`; CREATE TABLE `t_employee` (
`employeeid` int(10) NOT NULL COMMENT '员工id',
`employeename` varchar(20) NOT NULL COMMENT '员工姓名',
`employeesax` varchar(20) default NULL COMMENT '员工性别',
`employeepost` varchar(30) NOT NULL COMMENT '员工职务',
`employeedepartment` varchar(30) NOT NULL default '' COMMENT '员工所在部门',
PRIMARY KEY (`employeeid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; /*Data for the table `t_employee` */ insert into `t_employee`(`employeeid`,`employeename`,`employeesax`,`employeepost`,`employeedepartment`) values (1,'雷浩明','男','CEO','总裁办'),(2,'刘军','男','执行总裁','总裁办'),(3,'李海涛','男','总经理','总经办'),(4,'边肖','男','CTO','技术部'),(5,'柳青','女','经理','经营管理部'),(6,'高晓梅','女','财务','财务部'),(7,'曹舜水','男','研发经理','研发部'),(8,'董华','男','项目经理','研发部'),(9,'张军华','男','产品经理','产品部'),(10,'曾爽','女','测试经理','研发部'); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

项目代码结构截图:

项目需要的jar包下载地址:http://pan.baidu.com/s/1AWnPc。

mybatis连接mysql数据库实现的jdbc功能的更多相关文章

  1. Spring boot +mybatis 连接mysql数据库,获取JDBC失败,服务器时区价值”Oйu±e×¼e±¼的识别或代表多个时区

    报出的错误 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connec ...

  2. 五、使用druid管理数据库,mybatis连接mysql数据库

    简介:    使用 mybatis 连接 mysql 数据库, 一套简单的增删改查流程, 前台用 bootstrap, bootstrap-table 框架, 最后用 druid 监控数据库连接情况 ...

  3. (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

    大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...

  4. 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题

    一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...

  5. mybatis连接mysql数据库插入中文乱码

    对于MySQL数据库的乱码问题,有两种情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...

  6. Mybatis连接mysql数据库出现乱码

    对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...

  7. SpringBoot框架:使用mybatis连接mysql数据库完成数据访问(二)

    一.导入依赖包 1.在创建项目时勾选: 勾选SQL中的JDBC API.MyBatis Framework.MySQL Driver,创建项目后就会自动配置和引入这些包. 2.在pom.xml文件中添 ...

  8. Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能

    1 环境搭建 1.1 Python安装 1.2 MySQL环境搭建 1.3安装MySQLdb  2 具体实现 2.1 登陆界面 2.2 注册界面 2.3 具体实现部分代码   1 环境搭建 1.1 P ...

  9. SpringBoot 集成Mybatis 连接Mysql数据库

    记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...

随机推荐

  1. winform GDI基础(四)简单截屏

    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); G ...

  2. JavaScript面向对象编程入门

    来源极客网 function Person() { var _this = {} //创建一个空的对象,接着我们利用这个"空的对象"承载Person的属性和方法 _this.say ...

  3. Java从入门到放弃——05.修饰符static,final,权限修饰符

    本文目标 static final: 权限修饰符:public,private,protected,缺省 1.static 静态修饰符,被static修饰的变量或者方法会被加载进静态区内存,不需要创建 ...

  4. 「BZOJ 1791」「IOI 2008」Island「基环树」

    题意 求基环树森林所有基环树的直径之和 题解 考虑的一个基环树的直径,只会有两种情况,第一种是某个环上结点子树的直径,第二种是从两个环上结点子树内的最深路径,加上环上这两个结点之间的较长路径. 那就找 ...

  5. Win10每次开机总是自动弹出MSN网址导航如何取消

    Win10每次开机总是自动弹出MSN网址导航如何取消 近来有用户在升级Win10系统后,每次开机总是会自动弹出MSN中文网的网址导航.如果不想要开机打开MSN网址导航,那么应该怎么设置来取消呢?对此, ...

  6. nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx//conf/nginx.conf:117

    SSL相关的配置加到了nginx的配置文件中后,nginx竟然启动不起来了 于是用如下命令测试问题所在: /usr/local/nginx/sbin/nginx -c /usr/local/nginx ...

  7. php 过滤掉多维数组空值

    //过滤掉空值 function filter_array($arr, $values = ['',[]]){ foreach ($arr as $k => $v) { if (is_array ...

  8. doges

    dogse入门指南 Dogse作为游戏服务端引擎,目前只包含游戏服务端的核心部分,但这也是最核心的部分.它全部使用.net c#开发,充分兼顾了程序性能与代码编写的准确性与易用性,再加上以vs作为开发 ...

  9. 洛谷P2709 BZOJ 3781 小B的询问 (莫队)

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...

  10. 使用cookie实现自动登录

    一.从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到Activity.jsp login1.action(跳转到登录页面) /** 跳转到login(有积分排 ...