idea使用mybatis写增删改查
idea应用mybatis写增删改查
entity层
private Integer id;private String userCode;private String userName;private String userPassword;private Integer gender;private Date birthday;private String phone;private String address;private Integer userRole;private Integer createdBy;private Date creationDate;private Integer modifyBy;private Date modifyDate; public Integer getId() { return id;} public void setId(Integer id) { this.id = id;} public String getUserCode() { return userCode;} public void setUserCode(String userCode) { this.userCode = userCode;} public String getUserName() { return userName;} public void setUserName(String userName) { this.userName = userName;} public String getUserPassword() { return userPassword;} public void setUserPassword(String userPassword) { this.userPassword = userPassword;} public Integer getGender() { return gender;} public void setGender(Integer gender) { this.gender = gender;} public Date getBirthday() { return birthday;} public void setBirthday(Date birthday) { this.birthday = birthday;} public String getPhone() { return phone;} public void setPhone(String phone) { this.phone = phone;} public String getAddress() { return address;} public void setAddress(String address) { this.address = address;} public Integer getUserRole() { return userRole;} public void setUserRole(Integer userRole) { this.userRole = userRole;} public Integer getCreatedBy() { return createdBy;} public void setCreatedBy(Integer createdBy) { this.createdBy = createdBy;} public Date getCreationDate() { return creationDate;} public void setCreationDate(Date creationDate) { this.creationDate = creationDate;} public Integer getModifyBy() { return modifyBy;} public void setModifyBy(Integer modifyBy) { this.modifyBy = modifyBy;} public Date getModifyDate() { return modifyDate;} public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate;}
Dao层
IUserDao:(Interface)
//查询public List<User> getAllUser();//新增void addUser(User user);//删除void delUser(int id);//修改void modifyUser(User user);//新增时获得主键void addUserKey(User user);//模糊查询public List<User> getAllUsers();
IUserDao:(xml)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace需要指向接口全路径--> <mapper namespace="com.qzy.dao.IUserDao"> <!--id代表当前命名空间下(接口下)的唯一方法名 resultType代表返回值类型--> <select resultType="User" id="getAllUser"> select * from smbms_user </select> <insert parameterType="User" id="addUser"> insert into smbms_user values (DEFAULT,#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate},#{modifyBy},#{modifyDate}) </insert> <insert id="addUserKey" parameterType="com.qzy.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into smbms_user values (DEFAULT,#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate},#{modifyBy},#{modifyDate}) </insert> <delete id="delUser"> delete from smbms_user where id=#{xxx} </delete> <update id="modifyUser" parameterType="User"> update smbms_user set userName=#{userName} where id=#{id} </update> <select id="getAllUsers" resultType="User"> select * from smbms_user where userName like '%${value}%'; </select></mapper>
resources层mybatis-config.(xml)
<?xml version="1.0" encoding="UTF-8" ?>
省略......
<typeAliases> <!--单个设置别名--> <!-- <typeAlias type="com.qzy.entity.User" alias="User"/>--> <!--全局设置别名--> <package name="com.qzy.entity"/></typeAliases><!--log4j配置文件的读取(可以不写)-->
<settings> <setting name="logImpl" value="LOG4J"/></settings>
省略......
</configuration>tset层tset(class)
private static Logger log = Logger.getLogger(Test.class.getClass()); SqlSession sqlSession; @Before public void before() throws IOException { //System.out.println("执行之前========================"); log.info("执行之前========================"); //步骤一:读取大配置文件 String source="mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(source); //步骤二:创建工厂 SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); //步骤三:创建SqlSession核心对象 sqlSession = factory.openSession(); } @Test public void selectAllTest() throws IOException { //步骤四:查 List<User> getAllUser = sqlSession.selectList("getAllUser"); for (User user:getAllUser) { //System.out.println(user.getUserName()); log.info(user.getUserName()); } } //模糊 @Test public void getAllUsers() throws IOException{ List<User> userList=sqlSession.selectList("com.qzy.dao.IUserDao.getAllUsers","孙"); for (User u:userList){ ///System.out.println(u.getUserName()); log.info(u.getUserName()); } } //增 @Test public void addUser() throws IOException{ User user =new User(); user.setUserCode("hehe"); user.setUserName("牛皮呀"); user.setUserPassword("123123"); user.setGender(2); user.setBirthday(new Date()); user.setPhone("13215689594"); user.setAddress("北京市海淀区五道口北大青鸟"); user.setUserRole(2); user.setCreatedBy(1); user.setCreationDate(new Date()); user.setModifyBy(null); user.setModifyDate(null); sqlSession.insert("com.qzy.dao.IUserDao.addUser",user); sqlSession.commit(); //查看是否会返回自增的主键 //System.out.println(user.getId()); log.info(user.getId()); }//删@Testpublic void delUser() throws IOException{ sqlSession.delete("com.qzy.dao.IUserDao.delUser","17"); sqlSession.commit(); sqlSession.close(); //System.out.println("删除成功!"); log.info("删除成功!");}//改@Testpublic void modifyUser() throws IOException{ User user=new User(); user.setUserName("安其拉"); user.setId(16); sqlSession.update("com.qzy.dao.IUserDao.modifyUser",user); sqlSession.commit(); sqlSession.close(); //System.out.println("更改成功!"); log.info("更改成功!");} @After public void after(){ //System.out.println("执行之后===================================="); log.info("执行之后========================"); //最后关闭sqlSession释放资源 sqlSession.close(); }
log4j.(properties)
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppender###日志文件的了路径###log4j.appender.file.File=G:\\mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ######log4j.rootLogger=debug, stdout,filelog4j.logger.com.qzy.dao=debug, stdout,file
转载于:https://www.cnblogs.com/rzbwyj/p/11603955.html
idea使用mybatis写增删改查相关推荐
- MyBatis的增删改查操作
MyBatis的增删改查操作 简单实现一下MyBatis的增删改查,并且在控制台输出结果. 文章目录 MyBatis的增删改查操作 MyBatis的简单入门 一.查询操作 二.增加操作 三.修改操作 ...
- MyBatis实现增删改查功能(总)
文章目录 MyBatis实现增删改查功能(总) 一.创建数据库.表.数据 二.MyBatis的Maven项目创建 三.MyBatis的Maven模块创建 四.配置jar包pom.xml 五.MyBat ...
- mybatis实现增删改查xml配置和后端Java编程完整教程
mybatis实现增删改查xml配置和后端编程教程 SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8&quo ...
- mybatis完整增删改查入门实例
mybatis完整增删改查入门实例 编程步骤 ①创建Java工程. ②导入jar包. ③创建sqlMapConfig.xml配置文件,这是mybatis的入口. ④创建数据库,数据库表user_c,插 ...
- Spring+SpringMVC+Mybatis实现增删改查--(五)SSM修改员工页面搭建
Spring+SpringMVC+Mybatis实现增删改查--(五)SSM修改员工页面搭建 修改员工: 1.在index.jsp页面点击"编辑"弹出编辑对话框 2.去数据库查询部 ...
- Mybatis实现增删改查
文章目录 Mybatis实现增删改查 一.mybatis的配置 1.1 添加相应的jar包 1.2 配置mybatis.xml文件 1.3 创建数据库 1.4 创建实体类 1.5 创建接口实现的方法 ...
- (Mybatis)增删改查实现
文章目录 namespace 增删改查 1. Select 2. Insert 3. Update 4. Delete 万能Map 模糊查询 namespace namespace一定要与mapper ...
- 3. mysql的注解驱动的三种方式_上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。...
1.引入依赖 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot- ...
- springboot+springm vc+mybatis实现增删改查案例!
大家好,我是雄雄,欢迎关注微信公众号[雄雄的小课堂]. 前言 最近这几天都在看关于springboot的内容,每天新会获得点新收获,并且都总结发在公众号中:最后经过不懈努力,不断查找相关网页,解决各种 ...
最新文章
- 一步一步学习VirtualBox安装CentOS7和CentOS8
- 如何利用微信小游戏的分包加载机制突破4M代码包体积限制
- AcWing算法提高课 Level-3 第二章 搜索
- ​让人惊叹的Johnson-Lindenstrauss引理:应用篇
- 隐藏与显现_原神:芭芭拉的隐藏彩蛋你知道吗?对着游戏npc用技能就可显现
- 编译原理教程_6 LR分析法
- python怎么做一个动态烟花_简单烟花效果.py python firework demo
- QNX 系统日志设计
- 【经验分享】MAC系统自带截屏、录屏小工具
- python枚举算法流程图_python机器学习十大算法案例
- 设置计算机用户权限,计算机管理员权限在哪里设置_电脑系统如何设置管理员权限-win7之家...
- 【备忘录】transformers tokenizer.tokenize和tokenizer.encode
- Dynamics 365 窗体中设置可编辑的子网格
- html画布时钟添加背景图,用HTML5 Canvas 实现的 时钟
- 无人驾驶系统技术岗位分析(感知算法)
- 解决Idea使用git时commit特别慢的问题
- MySQL数据库入门学习教程(mysql基础+高级)
- C语言计算圆周长和面积
- 总结项目笔记Demo
- 又快又贱又失控?机器人教父Rodney Brooks预测AI未来32年历史进程