mybatis连接mysql数据库实现的jdbc功能
最近公司项目要使用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功能的更多相关文章
- Spring boot +mybatis 连接mysql数据库,获取JDBC失败,服务器时区价值”Oйu±e×¼e±¼的识别或代表多个时区
报出的错误 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connec ...
- 五、使用druid管理数据库,mybatis连接mysql数据库
简介: 使用 mybatis 连接 mysql 数据库, 一套简单的增删改查流程, 前台用 bootstrap, bootstrap-table 框架, 最后用 druid 监控数据库连接情况 ...
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...
- 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题
一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...
- mybatis连接mysql数据库插入中文乱码
对于MySQL数据库的乱码问题,有两种情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...
- Mybatis连接mysql数据库出现乱码
对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...
- SpringBoot框架:使用mybatis连接mysql数据库完成数据访问(二)
一.导入依赖包 1.在创建项目时勾选: 勾选SQL中的JDBC API.MyBatis Framework.MySQL Driver,创建项目后就会自动配置和引入这些包. 2.在pom.xml文件中添 ...
- 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 ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
随机推荐
- KindEditor 销毁与自动高度冲突解决
前提准备情况: KindEditor(KE) + easyUI 1.通过 EasyUI.Window 打开一个窗口,窗口中包含一个 KE编辑器:在次打开WIndow 的时候 KE会出现编辑器里面的 ...
- Kotlin第一篇 Hello Kotlin以及简单介绍。
首先需要一个编译器,我们使用Intellij IDE https://www.jetbrains.com/idea/download/#section=windows 下载下来安装好. 那么我们就来 ...
- weekly contest 115
958. Check Completeness of a Binary Tree Given a binary tree, determine if it is a complete binary t ...
- oracle多用户并发及事务处理
多用户并发访问 事务:作用于某些数据的一个不可分割的操作 锁:写锁.互斥锁(仅能被一个进程使用) 读锁.共享锁(可被多个进程使用) 更新丢失 脏读 不可重复读 幻影读 隔离级别: ...
- vue_cli下开发一个简单的模块权限系统之实现登录
因为我们需要和后端数据交互,所以我们需要安装axios,安装好以后在main.js引入 v-model是标识空间,v-on:click="doLogin"是登录事件 doLogin ...
- 老男孩Day8作业:FTP
1.作业需求 开发简单的FTP: 1. 用户登陆 2. 上传/下载文件 3. 不同用户家目录不同 4. 查看当前目录下文件 5. 充分使用面向对象知识 2.流程图 3.目录结构 4.代码区 bin目录 ...
- P2253 好一个一中腰鼓!
题意:给你一个序列,初始是0,每次一个操作,把一个数^=1 每次求出最长01串的长度 正解:线段树(虽然暴力能过) 对于每个区间,记录三个值 lmax,以l为首的01串长度 rmax,以r为尾的01串 ...
- 树状数组【bzoj3155】: Preprefix sum
3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了 ...
- 重装iTunes 错误代码42401 解决办法
昨晚手贱点击从iTunes 11升级到iTunes12,之后发现iTunes 12各种卡,简直不能忍,然后直接拉iTunes 12到AppClear,然后安装iTunes 11,安装完成之后打开iTu ...
- oracle随笔
Oracle新建数据库(新用户) 1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 passwo ...