In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the command usage of Mybatis addition, deletion, query and modification". In the daily operation, I believe that many people have doubts about the command usage of Mybatis addition, deletion, query and modification. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the command usage of Mybatis addition, deletion, query and modification. Next, please follow the editor to study!
Catalogue
I. explanation
2. Start to start
2.1 Database tables
2.1 create an entity class
2.2 create an interface
2.3Create XML
2.5 Test class
I. explanation
These two articles involve mapping Java entity classes, writing Mybatis for interfaces, and adding, deleting, querying and changing examples.
2. Launch 2.1 database tables
The last article seems to have lost the database creation statement.
-- DROP TABLE IF EXISTS `test`; CREATE TABLE `test` (`id` bigint (20) NOT NULL AUTO_INCREMENT, `name` varchar (200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `salary` decimal (10,2) NOT NULL, PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic -insert test data-INSERT INTO `test` VALUES (1, 'Xiaoming', 30000.00); 2.1 create entity class package entity;import java.math.BigDecimal / * * @ author found more highlights to follow the official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and searches, unworldly affectation * @ create 2021-08-25 22:05 * / public class TestEntity {private Long id; private String name; private BigDecimal salary; public Long getId () {return id } public void setId (Long id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public BigDecimal getSalary () {return salary;} public void setSalary (BigDecimal salary) {this.salary = salary } @ Override public String toString () {return "TestEntity {" + "id=" + id + ", name='" + name +'\'+ ", salary=" + salary +'}';}} 2.2 create interface package dao;import entity.TestEntity;import java.util.List / * * @ author found more highlights to follow the official account: Muzi's round-the-clock programming shares the feelings and learning of a programmer living at the bottom of the Internet who is doing additions, deletions and modifications * @ create 2021-08-25 22:07 * / public interface TestMapper {/ / add void save (TestEntity testEntity); / / modify void update (TestEntity testEntity) / / delete only one parameter here, so do not use @ Param or void delete (Long id) such as Map custom entity class; / / query TestEntity get (Long id) according to the primary key; / / query all data List list (); / / vaguely query List listByNameLike (String name) by name;} 2.3 create XML
Mybatis-config.xml
TestMapper.xml
INSERT INTO `test` (`name`, `salary`) VALUE (# {name}, # {salary}) Delete from test where id = # {id} select * from test where id = # {id} select * from test select * from test where name like CONCAT ('%', # {name},'%') update test set name = # {name}, salary = # {salary} where id = # {id} 2.5 test class
Take a look at the database data first.
New data
Import dao.TestMapper;import entity.TestEntity;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 java.io.InputStream;import java.math.BigDecimal;import java.util.List;import java.util.Map / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and searches, unworldly affectation * @ create 2021-08-25 21:26 * / public class TestMain {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); try (SqlSession session = sqlSessionFactory.openSession ()) {/ / get Mapper through sesson this Mapper will program Mybatis's agent Mapper TestMapper mapper = session.getMapper (TestMapper.class); System.out.println (mapper); / / 1. Insert data TestEntity entity = new TestEntity (); entity.setName ("Little Moonbird"); entity.setSalary (new BigDecimal (50000)); mapper.save (entity); TestEntity entity02 = new TestEntity (); entity02.setName ("Xiaoqiang 01"); entity02.setSalary (new BigDecimal (50000)); mapper.save (entity02) TestEntity entity03 = new TestEntity (); entity03.setName ("Xiaoqiang 02"); entity03.setSalary (new BigDecimal (50000)); mapper.save (entity03); / / manually submit session.commit ();}
After execution, view the database data:
Query data according to Id
Import dao.TestMapper;import entity.TestEntity;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 java.io.InputStream / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and searches, unworldly affectation * @ create 2021-08-25 21:26 * / public class TestMain {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); try (SqlSession session = sqlSessionFactory.openSession ()) {/ / get Mapper through sesson this Mapper will program Mybatis's agent Mapper TestMapper mapper = session.getMapper (TestMapper.class); System.out.println (mapper); / / 1. Query data according to Id TestEntity testEntity = mapper.get (1L); System.out.println ("query data is:" + testEntity);}
Output result:
Update data
Change the salary of Little Moon Bird to 40000.
Import dao.TestMapper;import entity.TestEntity;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 java.io.InputStream;import java.math.BigDecimal / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and searches, unworldly affectation * @ create 2021-08-25 21:26 * / public class TestMain {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); try (SqlSession session = sqlSessionFactory.openSession ()) {/ / get Mapper through sesson this Mapper will program Mybatis's agent Mapper TestMapper mapper = session.getMapper (TestMapper.class); System.out.println (mapper); / / Update TestEntity entityUpdate = new TestEntity (); entityUpdate.setId (40L) EntityUpdate.setName (Little Moonbird); entityUpdate.setSalary (new BigDecimal (40000)); mapper.update (entityUpdate); session.commit ();}
After execution, view the database data:
Query all data
Import dao.TestMapper;import entity.TestEntity;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 java.io.InputStream;import java.util.List / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and searches, unworldly affectation * @ create 2021-08-25 21:26 * / public class TestMain {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); try (SqlSession session = sqlSessionFactory.openSession ()) {/ / get Mapper through sesson this Mapper can program Mybatis's agent Mapper TestMapper mapper = session.getMapper (TestMapper.class); System.out.println (mapper); / / query list List list = mapper.list () If (list.size () > 0) {for (int I = 0; I)
< list.size(); i++) { System.out.println(list.get(i)); } } } }} 输出结果: 根据名称查询数据 import dao.TestMapper;import entity.TestEntity;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 java.io.InputStream;import java.util.List;/** * @author 发现更多精彩 关注公众号:木子的昼夜编程 * 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作 * @create 2021-08-25 21:26 */public class TestMain { public static void main(String[] args) throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession()) { // 通过sesson获取Mapper 这个Mapper会编程Mybatis的代理Mapper TestMapper mapper = session.getMapper(TestMapper.class); System.out.println(mapper); // 根据名称模糊查询列表 List list = mapper.listByNameLike("强"); if (list.size() >0) {for (int I = 0; I < list.size (); iTunes +) {System.out.println (list.get (I));}
Output result:
Delete data
Import dao.TestMapper;import entity.TestEntity;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 java.io.InputStream;import java.util.List / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and searches, unworldly affectation * @ create 2021-08-25 21:26 * / public class TestMain {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); try (SqlSession session = sqlSessionFactory.openSession ()) {/ / get Mapper through sesson this Mapper will program Mybatis's agent Mapper TestMapper mapper = session.getMapper (TestMapper.class); System.out.println (mapper); / / Delete data mapper.delete (1L) Session.commit ();}
After execution, view the database data:
Project structure:
At this point, the study of "Mybatis addition, deletion, query and modification command usage" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.