In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to add, delete, modify and check Mybatis, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Mybatis to add, delete, modify and check
I. configuration of mybatis 1.1 add the corresponding jar package
Add the core jar package of mybatis and the dependent jar package under the lib folder
Also add the mysql driver jar package under the lib folder
1.2 configure mybatis.xml file 1.3 create database
Note: when designing the database, id is of type int and is set as the primary key. Automatically increments.
Create entity class package com.sxt.domain;public class User {private Integer id; private String name; private String address; private String sex; public User () {} / / insert public User (String name, String address, String sex) {super (); this.name = name; this.address = address; this.sex = sex } / / query public User (Integer id, String name, String address, String sex) {super (); this.id = id; this.name = name; this.address = address; this.sex = sex;} 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;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex } @ Override public String toString () {return "User [id=" + id + ", name=" + name + ", address=" + address + ", sex=" + sex + "]";}} 1.5 create interface implementation methods package com.sxt.mapper;import com.sxt.domain.User;import java.util.List;public interface UserMapper {public void add (User user); public void update (User user); public void delete (Integer id) / / deleted constructor public User queryById (Integer id); / / query constructor public List queryAll (); public List queryLike (User user) / / Fuzzy query} 1.6 configure UserMapper.xml file insert into user (name,address,sex) values (# {name}, # {address}, # {sex}) update user set name=# {name}, address=# {address} Sex=# {sex} where id=# {id} delete form user where id=# {value} select * from user where id=# {value} select * from user where name like "%" # {name} "%" 1.7 method package com.sxt.utils for creating MybatisUtils Import java.io.InputStream;import javax.websocket.Session;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MybatisUtils {static InputStream is=MybatisUtils.class.getResourceAsStream ("/ mybatis.xml"); / / get SqlSessionFactory static SqlSessionFactory factory=new SqlSessionFactoryBuilder () .build (is); / / get session public static SqlSession openSession () {return factory.openSession () } / / close session public static void closeSession (SqlSession session) {/ / submit session.commit (); / / close session.close ();}} 2. Add, delete, modify and check Mybatis 2.1 add package com.sxt.text;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.sxt.domain.User;import com.sxt.mapper.UserMapper Import com.sxt.utils.MybatisUtils;public class mybatisTest {public static void main (String [] args) {SqlSession session=MybatisUtils.openSession (); UserMapper userMapper=session.getMapper (UserMapper.class); / / TODO Auto-generated method stub User user=new User ("Xiaohua", "Wuhan", "male"); userMapper.add (user); System.out.println ("chen"); MybatisUtils.closeSession (session);}
2.2 modify package com.sxt.text;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.sxt.domain.User;import com.sxt.mapper.UserMapper;import com.sxt.utils.MybatisUtils;public class mybatisTest {/ / SqlSession session=MybatisUtils.openSession (); / / UserMapper userMapper=session.getMapper (UserMapper.class) / / @ Test// public void initData () {public static void main (String [] args) {SqlSession session=MybatisUtils.openSession (); UserMapper userMapper=session.getMapper (UserMapper.class); / / TODO Auto-generated method stub// User user=new User ("Xiaoming", "Wuhan", "male"); User user=new User (2, "floret", "Hebei", "female"); userMapper.update (user) MybatisUtils.closeSession (session);}
2.3 query package com.sxt.text;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.sxt.domain.User;import com.sxt.mapper.UserMapper;import com.sxt.utils.MybatisUtils;public class mybatisTest {/ / SqlSession session=MybatisUtils.openSession (); / / UserMapper userMapper=session.getMapper (UserMapper.class) / / @ Test// public void initData () {public static void main (String [] args) {SqlSession session=MybatisUtils.openSession (); UserMapper userMapper=session.getMapper (UserMapper.class); / / TODO Auto-generated method stub// User user=new User ("Xiaoming", "Wuhan", "male"); / / User user=new User (2, "Xiaohua", "Hebei", "female"); List list=userMapper.queryAll () System.out.println (list); MybatisUtils.closeSession (session);}
Delete package com.sxt.text;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.sxt.domain.User;import com.sxt.mapper.UserMapper;import com.sxt.utils.MybatisUtils;public class mybatisTest {/ / SqlSession session=MybatisUtils.openSession (); / / UserMapper userMapper=session.getMapper (UserMapper.class) / / @ Test// public void initData () {public static void main (String [] args) {SqlSession session=MybatisUtils.openSession (); UserMapper userMapper=session.getMapper (UserMapper.class); / / TODO Auto-generated method stub// User user=new User ("Xiaoming", "Wuhan", "male"); / / User user=new User (2, "Xiaohua", "Hebei", "female") / / List list=userMapper.queryAll (); / / System.out.println (list); userMapper.delete (3); MybatisUtils.closeSession (session);} Thank you for reading this article carefully. I hope the article "how to add, delete, modify and query Mybatis" shared by the editor will be helpful to you. At the same time, I also hope you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.