大连企业免费建站,qq是哪家公司的产品,网店运营模式有哪些,软文写作范文500字引用#xff1a;MyBatis的删除、修改、插入操作#xff01;#xff01;#xff01;-CSDN博客的准备工作#xff01;#xff01;#xff01;#xff08;准备工作都一样只不过文件名称有所不同#xff09; 1.利用原始DAO开发#xff0c;查询所有的信息。 UserDao#…引用MyBatis的删除、修改、插入操作-CSDN博客的准备工作准备工作都一样只不过文件名称有所不同 1.利用原始DAO开发查询所有的信息。 UserDao
package com.by.dao;import com.by.pojo.User;import java.util.List;public interface UserDao {ListUser findAll();}UserMapper.xml:
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.by.dao.UserDaoselect idfindAll resultTypecom.by.pojo.Userselect * from user/select
/mapper
与Mapper开发有所不同的是原始Dao开发需要有接口的实现类
UserDaoImpl 接口的实现类的写法你需要注意调用的方法以及传递的参数。参数为全类名方法名
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.by.dao;import com.by.pojo.User;
import org.apache.ibatis.session.SqlSession;import java.util.List;public class UserDaoImpl implements UserDao{private SqlSession sqlSession;public UserDaoImpl(SqlSession sqlSession) {this.sqlSession sqlSession;}Overridepublic ListUser findAll() {return sqlSession.selectList(com.by.dao.UserDao.findAll);}}
测试类
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.by;import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
import com.by.pojo.User;
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 org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** pProject: mybatis - MyBatisTest/p* pPowered by scl On 2023-12-18 11:44:53/p* p描述p** author 孙臣龙 [1846080280qq.com]* version 1.0* since 17*/
public class MyBatisTest {Testpublic void testFindAll() throws IOException {// 加载配置文件String resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);// 创建sqlSessionFActorySqlSessionFactory sessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 获得数据的绘画实例SqlSession sqlSession sessionFactory.openSession();UserDao userDao new UserDaoImpl(sqlSession);ListUser userList userDao.findAll();for (User str : userList) {System.out.println(str);}inputStream.close();sqlSession.close();}} 2.根据id查询信息 UserDao
package com.by.dao;import com.by.pojo.User;
import java.util.List;public interface UserDao {User getUserById(Integer id);
}UserDao.xml:
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.by.dao.UserDaoselect idgetUserById resultTypecom.by.pojo.Userselect *from user where id#{id};/select
/mapper
UserDaoImpl 接口的实现类的写法你需要注意调用的方法以及传递的参数。参数为全类名方法名
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.by.dao;import com.by.pojo.User;
import org.apache.ibatis.session.SqlSession;
import java.util.List;public class UserDaoImpl implements UserDao{private SqlSession sqlSession;public UserDaoImpl(SqlSession sqlSession) {this.sqlSession sqlSession;}Overridepublic User getUserById(Integer id) {return sqlSession.selectOne(com.by.dao.UserDao.getUserById,id);}
}
测试类
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.by;import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
import com.by.pojo.User;
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 org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class MyBatisTest {Testpublic void getUserById() throws IOException {// 加载配置文件String resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);// 创建sqlSessionFActorySqlSessionFactory sessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 获得数据的绘画实例SqlSession sqlSession sessionFactory.openSession();UserDao userDao new UserDaoImpl(sqlSession);User user userDao.getUserById(42);System.out.println(user);inputStream.close();sqlSession.close();}}