In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "MyBatis Framework Zero Foundation Quick start case Analysis". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the "MyBatis Framework Zero Foundation Quick start case Analysis".
I. create databases and tables
Database name ssm, data table student
Mysql > create database ssm;Query OK, 1 row affected (0.01 sec) mysql > use ssmDatabase changedmysql > CREATE TABLE `student` (- > `id` int (11) NOT NULL,-> `name` varchar (255) DEFAULT NULL,-> `email` varchar (255) DEFAULT NULL,-> `age` int (11) DEFAULT NULL,-> PRIMARY KEY (`id`)-> ENGINE=InnoDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected, 3 warnings (0.03 sec)
2. Create a maven project
1. Pom.xml adds maven coordinates
Junit junit 4.11 test org.mybatis mybatis 3.5.1 mysql mysql-connector-java 5.1.9
2. Add the maven plug-in
Src/main/java * * / * .properties * * / .xml false maven-compiler-plugin 3.1 1.8 1.8 III, code writing 1, writing Student entity classes
Create a package com.Example.domain and create a Student class in the package
Package com.bjpowernode.domain;/** *
Description: entity class
*
Company: http://www.bjpowernode.com * / public class Student {/ / attribute name is the same as column name private Integer id; private String name; private String email; private Integer age; / / set, get, toString} 2, write DAO interface StudentDao
Create com.Example.dao package, create StudentDao interface
Package com.bjpowernode.dao;import com.bjpowernode.domain.Student;import java.util.List;/* *
Description: Dao interface
*
Company: http://www.bjpowernode.com * / public interface StudentDao {/ * query all data * / List selectStudents ();} 3. Write the DAO interface Mapper mapping file StudentDao.xml.
Create the file StudentDao.xml in the dao package
Want the StudentDao.xml file name to be the same as the interface StudentDao, case-sensitive.
Select id,name,email,age from student 4. Create the MyBatis master configuration file
Create a resources directory under the project src/main, and set the resources directory to resources root
Create a master profile: name is mybatis.xml
Support for url in Chinese
Jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
4. Create a test class for testing 1. Create a test class MyBatisTest
Src/test/java/com/Example/ creates a MyBatisTest.java file
/ * * getting started with mybatis * / @ Testpublic void testStart () throws IOException {/ / 1.mybatis main configuration file String config = "mybatis-config.xml"; / / 2. Read the configuration file InputStream in = Resources.getResourceAsStream (config); / / 3. Create a SqlSessionFactory object to get SqlSession SqlSessionFactory factory = new SqlSessionFactoryBuilder (). Build (in); / / 4. Get SqlSession,SqlSession to execute the sql statement SqlSession session = factory.openSession (); / / 5. Execute selectList () List studentList of SqlSession = session.selectList ("com.bjpowernode.dao.StudentDao.selectStudents"); / / 6. Loop out the query results studentList.forEach (student-> System.out.println (student)); / / 7. Close SqlSession, release resource session.close ();} List studentList = session.selectList ("com.bjpowernode.dao.StudentDao.selectStudents"); approximate equivalent jdbc code Connection conn = get connection object String sql= "select id,name,email,age from student" PreparedStatement ps = conn.prepareStatement (sql); ResultSet rs = ps.executeQuery (); 2. Configure log function
The mybatis.xml file is added to the log configuration, and the executed sql statements and parameters can be output in the console.
5. Add, delete and modify operation insert operation
(1) methods in StudentDAO interface
Int insertStudent (Student student)
(2) StudentDAO.xml adds sql statement
Insert into student (id,name,email,age) values (# {id}, # {name}, # {email}, # {age})
(3) add test methods.
@ Testpublic void testInsert () throws IOException {/ / 1.mybatis main configuration file String config = "mybatis-config.xml"; / / 2. Read the configuration file InputStream in = Resources.getResourceAsStream (config); / / 3. Create the SqlSessionFactory object SqlSessionFactory factory = new SqlSessionFactoryBuilder (). Build (in); / / 4. Get SqlSession SqlSession session = factory.openSession (); / / 5. Create objects to save data: Student student = new Student (); student.setId (1005); student.setName ("Zhang Li"); student.setEmail ("zhangli@163.com"); student.setAge (20); / / 6. Insert insert int rows = session.insert ("com.bjpowernode.dao.StudentDao.insertStudent", student); / / 7. Commit transaction session.commit (); System.out.println ("increase the number of rows of records:" + rows); / / 8. Close SqlSession session.close ();} Thank you for your reading, the above is the content of "MyBatis Framework Zero Foundation Quick start case Analysis". After the study of this article, I believe you have a deeper understanding of the problem of MyBatis Framework Zero Foundation Quick start case Analysis, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.