In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to write the mapping file mapper.xml of mybatis". In the daily operation, I believe that many people have doubts about how to write the mapping file mapper.xml of mybatis. 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 of "how to write the mapping file mapper.xml of mybatis". Next, please follow the editor to study!
When learning mybatis, we usually write this in the mapping file:
Select * from student where id=# {id} select * from student where gid=# {gid}
Then the implementation of dao is writing a class to implement the interface of dao. But this needs to be written in the implementation as follows:
Package com.qbd.service;import org.apache.ibatis.session.SqlSession;import org.apache.log4j.Logger; import com.qbd.mapper.StudentMappers;import com.qbd.model.Student;import com.qbd.util.SqlSessionFactoryUtil; public class StudentService {private static Logger logge=Logger.getLogger (StudentService.class); public static void main (String [] args) {SqlSession sqlSession=SqlSessionFactoryUtil.getSqlSession (); StudentMappers studentMappers= (StudentMappers) sqlSession.getMapper (StudentMappers.class); Student student=new Student () Student.setName ("22"); student.setAge (2); int s=studentMappers.add (student); sqlSession.commit (); if (s > 0) {logge.info ("success"); System.out.println ("success");}
To read the configuration file
Another way is to:
Directly in this part of mapper.xml, write dao as follows
Select * from user and uname like # {uname} limit # {start} # {size} select count (*) from user and uname like # {uname} select * from user where uname=# {uname} and upassword=# {upassword} delete from user where uid=# {uid} update user Uname=# {uname} Upassword=# {upassword}, upower=# {upower}, where uid=# {uid} insert into user values (null,# {uname}, # {upassword}, # {upower})
Then you don't have to write the implementation of dao to disable it in service. Mybatis will map mapper.xml to dao by default.
Then the following dao can write:
Package com.qbd.ssm.dao; import java.util.List;import java.util.Map; import org.apache.ibatis.annotations.Delete; import com.qbd.ssm.model.User; public interface UserDao {public List getAll (); public User getUser (User user); public int delete (User user); public int update (User user); public int add (User user); public List find (Map map); public Long getTotal (Map map);}
Service wrote like this.
Package com.qbd.ssm.service;import java.util.List;import java.util.Map;import com.qbd.ssm.model.User; public interface UserService {public List getAll (); public User getUser (User user); public int delete (User user); public int update (User user); public int add (User user); public List find (Map map); public Long getTotal (Map map);}
Implementation of service:
Package com.qbd.ssm.serviceimpl;import java.util.List;import java.util.Map;import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.qbd.ssm.dao.UserDao;import com.qbd.ssm.model.User;import com.qbd.ssm.service.UserService; @ Service ("userService") public class UserServiceImpl implements UserService {private UserDao userDao; public UserDao getUserDao () {return userDao } @ Resource public void setUserDao (UserDao userDao) {this.userDao = userDao;} public List getAll () {/ / TODO Auto-generated method stub return userDao.getAll ();} public User getUser (User user) {/ / TODO Auto-generated method stub return userDao.getUser (user) } public int delete (User user) {/ / TODO Auto-generated method stub return userDao.delete (user);} public int update (User user) {/ / TODO Auto-generated method stub return userDao.update (user);} public int add (User user) {/ / TODO Auto-generated method stub return userDao.add (user) } public List find (Map map) {/ / TODO Auto-generated method stub return userDao.find (map);} public Long getTotal (Map map) {/ / TODO Auto-generated method stub return userDao.getTotal (map);}}
In this case, userDao cannot be written wrong. Spring will be injected according to name.
At this point, the study on "how to write the mapping file mapper.xml of mybatis" is over. I hope to be able to solve your 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.