Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build annotations in Mybatis

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to build annotations in Mybatis. The content is very detailed. Interested friends can refer to it for reference. I hope it can help you.

#I. Introduction to Mybatis Framework

MyBatis was originally an apache open source project iBatis. In 2010, this project was migrated from apache software foundation to google code and renamed MyBatis. Moved to Github in November 2013.

iBATIS, a combination of "internet" and "abatis," is a Java-based persistence layer framework. iBATIS provides persistence layer frameworks including SQL Maps and Data Access Objects (DAOs).

II. Prepare Jar Pack

Create a lib folder in the root directory of the project to store jar files

三.准备配置文件1.c3p0.propertis

此配置文件主要是用 c3p0连接数据库的信息,修改其中的driverClass(连接驱动)、url、user以及密码。

注意:dirverClass是数据库连接驱动,不同版本,不同数据库连接驱动都不同,我用的是mysql5.7版本,命名必须是这个,不要问,问就是规定!!

2.log4j2.xml

此配置文件主要是日志的打印以及保存,找到 标签, (日志保存路径) (当前项目名)。

四.创建Mybatis核心配置文件

在src根目录下新建一个mybatis-cofig.xml(名字随意)的配置文件

1.配置log4j 2.配置c3p0数据源 3.创建实体类

Teacher实体类

package com.hsiao.entiy;import java.util.Date;public class Student { private Long sid; private String sname; private Date sdate; private Long tid; private Teacher tvo; public Student() { super(); // TODO Auto-generated constructor stub } public Student(Long sid, String sname, Date sdate, Long tid, Teacher tvo) { super(); this.sid = sid; this.sname = sname; this.sdate = sdate; this.tid = tid; this.tvo = tvo; } public Long getSid() { return sid; } public void setSid(Long sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Date getSdate() { return sdate; } public void setSdate(Date sdate) { this.sdate = sdate; } public Long getTid() { return tid; } public void setTid(Long tid) { this.tid = tid; } public Teacher getTvo() { return tvo; } public void setTvo(Teacher tvo) { this.tvo = tvo; } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", sdate=" + sdate + ", tid=" + tid + ", tvo=" + tvo + "]"; } }4.创建dao层以及实现类

新建dao包-->新建teacherDao接口

-->在dao包中新建impl包

-->在impl中新建teacherDaoImpl实现teacherDao接口

5.在核心配置文件中加入实体类的映射 6.创建session工厂

在factory包中新建一个MybatisSessionFactory工厂类

package com.hsiao.factory;import java.io.InputStream;import org.apache.ibatis.session.ExecutorType;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class MybatisSessionFactory { //创建日志追踪器 private static Logger log=LogManager.getLogger(); private static SqlSessionFactory sqlSessionFactory; /** * 加载mybatis-config.xml配置文件 */ static { InputStream ins= MybatisSessionFactory.class.getClass().getResourceAsStream("/mybatis-config.xml"); sqlSessionFactory=new SqlSessionFactoryBuilder().build(ins); } public static SqlSession getSession() { if(sqlSessionFactory==null) { throw new NullPointerException("session 工厂创建失败!"); } //设置BATCH 批处理模式 fasle代表是事务非自动提交 SqlSession session=sqlSessionFactory.openSession(ExecutorType.BATCH,false); return session; }}7.测试获取连接public static void main(String...args) { SqlSession session=MybatisSessionFactory.getSession(); log.debug("获取Connection对象"+session.getConnection()); }

测试无异常在进行下一步。

8. Encapsulate the result set in TeacherVO.xml 9. Write sql statement tags in TeacherVO.xml SELECT TID,TNAME,SUBJECT FROM TEACHER10.dao layer write abstract method package com.hsiao.dao;import com.hsiao.entiy.Teacher;import java.util.List;public interface TeacherDao { public List selectAllTeacher();}

Note: The method name of the dao layer method is consistent with the id of the select tag in the object mapping file (TeacherVO.xml).

11.dao layer implementation class writing method and test package com.hsiao.dao.impl;import com.hsiao.dao.TeacherDao;import com.hsiao.entiy.Teacher;import com. hsiao.factory.MybatisSessionFactory;import org.apache.ibatis.session.SqlSession;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import java.util.List;public class TeacherDaoImplements TeacherDao {pl private static Logger log= LogManager.getLogger(); @Override public List selectAllTeacher() { SqlSession session= MybatisSessionFactory.getSession(); List list= session.selectList("selectAllTeacher"); session.close(); return list; } public static void main(String[] args) { TeacherDaoImpl dao=new TeacherDaoImpl(); List list=dao.selectAllTeacher(); list.forEach((m)->log.debug(m)); }}

How to build annotations in Mybatis is shared here. I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report