利用mybatis连接mysql数据库进行数据的操作

摘要:
--让程序识别jdbc。属性--˃˂----˃以学校管理学生为例。实体层/**CreatedbyAdministrator 2017年9月14日。*/publicclassStudent{privateInteger;privateStringname;publicIntegerId(){returnid;}publicvoidsetId{this.id=id;}publicStringgetName(){returnname;}publicvoidsetName{this.name=name;}}接口/***CreatedbyAdministrator 2017年9月14日。*/publicinterfaceIStudentDAO{/***查询所有*@return*/publicList<Student>getAll();/***添加*@paramstudent*@return*/publicintaddStudentthrowsException;}一个小配置文件。此文件也以结尾。xml格式。代码如下:˂?

整体结构如下:

利用mybatis连接mysql数据库进行数据的操作第1张

首先写大配置,该配置的作用是连接数据库。

   可以将连接数据库的方法单独提出来,写在jdbc.propterties中,代码如下:

jdbc.driver=com.mysql.jdbc.Driver   //加载驱动
jdbc.url=jdbc:mysql://localhost:3306/school //连接mysql数据库
jdbc.username=root //数据库用户名
jdbc.password=123156 //数据库密码

大配置文件的名称一般为mybatis-config.xml

mybatis-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> <!--根节点 -->
<!--让程序识别到jdbc.propterties -->
<properties resource="jdbc.properties">
</properties>
<environments default="development">
<!--数据库环境 -->
<environment id="development">
<!--事务管理方案 -->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--映射文件:描述某个实体和数据库表的对应关系 -->
<!--关联小配置 -->
<mappers>
<mapper resource="cn/happy/dao/IStudentDAO.xml" />
<!-- <package name="cn.happy.dao"></package>-->
</mappers>
</configuration>

下面以学校管理学生为例
实体层
/**
* Created by Administrator on 2017/9/14.
*/
public class Student {
private Integer id;
private String name;

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;
}
}

接口
/**
* Created by Administrator on 2017/9/14.
*/
public interface IStudentDAO {

/**
* 查询所有
* @return
*/
public List<Student> getAll();

/**
* 新增
* @param student
* @return
*/
public int addStudent(Student student) throws Exception;
}


小配置文件 该文件同样是以.xml结尾

代码如下:
<?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="cn.happy.dao.IStudentDAO">
<!--查询所有-->
<select id="getAll" resultType="cn.happy.entity.Student">
select *from student
</select>
<!--新增学生-->
<select id="addStudent" resultType="cn.happy.entity.Student">
insert into student(name) value(#{name})
</select>
</mapper>


测试类
public class testStudent {
@Test
/**
* 查询
*/
public void getAll() throws Exception{
String resource = "mybatis-config.xml";
//将硬盘中的xml变成一个输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
//使用流对象创建一个会话工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
//session就是程序员与数据库交互的入口
SqlSession session = sf.openSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Student> all = mapper.getAll();
for (Student item:all){
System.out.println(item.getName());
}
session.commit();
//关闭会话,释放资源
session.close();
}

/**
* 新增
* @throws Exception
*/
public void inserts() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sf.openSession();
Student student=new Student();
student.setName("张飞");
int count = session.insert("cn.happy.dao.IStudentDAO.addStudent",student);
System.out.println(count);
session.commit();
session.close();
}

}
 
这里只写了新增和查询所有,删除和修改类似,只有需要做小的改动,因此这里就不写出来了。

 
 
 

免责声明:文章转载自《利用mybatis连接mysql数据库进行数据的操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇开源的mqtt服务器Android开发之权限列表下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

将Mongodb的表导入到Hive中

1.官方文档:https://docs.mongodb.com/ecosystem/tools/hadoop/ 2.Hive介绍:   Hive特点:     1.hive是一个数据仓库,和oracle,mysql等数据仓库相比,它底层依赖于hdfs。   2.hive是sql解析引擎,将sql语句转换成Map/Reduce任务,然后在hadoop hdf...

如何更改SQL Server2008默认数据库的存储路径

1.安装SQl Server过程中,修改路径(因为我安装的时候,忘记改路径了,所以没截下图来,黄色部分是真正的标注): 当然了,也可以修改共享功能目录 以及修改实例根目录 不过,我也不知道共享功能目录,实例根目录装的啥。 2.打开SQL Server2008,在图形界面里修改数据库的存储路径:  右击“对象资源管理器”,然后单击“属性”,在单击“数...

解决MySQL数据库同步1236错误

1、报错如下: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged bin...

linux下,达梦数据库8 备份还原

  一 数据库备份相关概念 备份就是数据库在某一个时间点的副本 数据库备份目的:防止数据丢失,防止天灾人祸(地震、火灾、人为操作、硬件故障) 备份主要有物理备份和逻辑备份 物理备份主要是备份使用过的有效的数据页,逻辑备份主要是备份数据库对象(表,索引,视图,存储过程等) 物理备份:分为联机备份和脱机备份;完全备份和增量备份 完全备份:备份的是整个数据库或者...

mybatis plus 联合查询

在xml中只需要需要写如下的代码即可实现分页: <select parameterType="map" resultType="com.test.mybatisplus.pojo.User"> SELECT <include refid="Base_Column_List" />...

大数据是什么

大数据是什么 大数据本身是一个抽象的概念。从一般意义上讲,大数据是指无法在有限时间内用常规软件工具对其进行获取、存储、管理和处理的数据集合。 目前,业界对大数据还没有一个统一的定义,但是大家普遍认为,大数据具备 Volume、Velocity、Variety 和 Value 四个特征,简称“4V”,即数据体量巨大、数据速度快、数据类型繁多和数据价值密度低,...