首页 > 其他分享 >10.24

10.24

时间:2023-12-18 20:37:22浏览次数:31  
标签:10.24 id emp time import public Emp

package com.itheima.mybatisdatabaseexample.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private short gender;
    private String imag;
    private short job;
    private LocalDate entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

}
复制代码

 

定义了一个EmpMapper的接口

EmpMapper.java

复制代码
package com.itheima.mybatisdatabaseexample.mapper;

import com.itheima.mybatisdatabaseexample.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Mapper

public interface EmpMapper {
    //删除数据
    @Delete("delete from mybaits.emp where id=#{id}")
    public void delete(Integer id);

    @Options(keyProperty = "id", useGeneratedKeys = true)
    @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
            " VALUES (#{username},#{name},#{gender},#{imag},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
    public void insert(Emp emp);

    @Update("update  emp set username=#{username},name=#{name},gender=#{gender},image=#{imag},job=#{job},entrydate=#{entrydate},dept_id=#{deptId},update_time=#{updateTime} where id=#{id};")
    public void update(Emp emp);
//@Select("select *  from emp where id=#{id}")
//    public Emp select(Integer id);


//查询完善的方式一:起别名
//@Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id=#{id}")
//public Emp select(Integer id);


//方式二 通过@Result @Results手动注解封装
//    @Results({
//            @Result(property = "deptId",column = "dept_id"),
//            @Result(property = "createTime",column = "create_time"),
//            @Result(property = "updateTime",column = "update_time"),
//    })
//    @Select("select *  from emp where id=#{id}")
//    public Emp select(Integer id);


//    方案三:开启mybatis的驼峰命名自动映射开关

    @Select("select *  from emp where id=#{id}")
    public Emp select(Integer id);
//多条件查询
//    @Select("select * from emp where name like '%张%' " +
//            "and gender=1 and entrydate between'2010-01-01' and '2023-01-01'" +
//            " order by update_time desc ;")
//    public List<Emp>list();

    //    使用字符串拼接
    @Select("select * from emp where name like concat('%','张','%')" +
            "and gender=1 and entrydate between'2010-01-01' and '2023-01-01'" +
            " order by update_time desc ;")
    public List<Emp> list();

//    @Select("select * from emp where name like '%${name}%' " +
//            "and gender=#{gender} and entrydate between#{begin} and #{end}" +
//            " order by update_time desc ;")
//    public List<Emp>list(String name,short gender, LocalDate begin, LocalDate end);
}
复制代码

同时在测试类中定义函数来实现函数

MybatisDatabaseExampleApplicationTests.java

复制代码
package com.itheima.mybatisdatabaseexample;

import com.itheima.mybatisdatabaseexample.mapper.EmpMapper;
import com.itheima.mybatisdatabaseexample.pojo.Emp;
import org.apache.ibatis.annotations.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@SpringBootTest
class MybatisDatabaseExampleApplicationTests {
    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testDelete() {
        empMapper.delete(17);
    }

    @Test
    public void insert() {
        Emp emp = new Emp();
        emp.setUsername("Tom1");
        emp.setName("汤姆1");
        emp.setGender((short) 1);
        emp.setImag("1.jpg");
        emp.setJob((short) 1);
        emp.setEntrydate(LocalDate.now());
        emp.setDeptId(1);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        System.out.println(emp.getId());
    }

    @Test
    public void update() {
        Emp emp = new Emp();
        emp.setUsername("Tom5");
        emp.setName("汤姆5");
        emp.setGender((short) 1);
        emp.setImag("3.jpg");
        emp.setJob((short) 2);
        emp.setEntrydate(LocalDate.of(2022, 10, 15));
        emp.setDeptId(2);
        emp.setUpdateTime(LocalDateTime.now());
        emp.setId(20);
        empMapper.update(emp);
    }

    @Test
    public void select() {
        Emp emp = empMapper.select(7);
        System.out.println(emp);
    }


    @Test
    public void selectByCondition() {
        List<Emp> empList = empMapper.list();
        System.out.println(empList);
    }
//    @Test
//    public void selectByCondition()
//    {
//List<Emp>empList=empMapper.list("张",(short)1,LocalDate.of(2010,01,01),LocalDate.of(2020,01,01));
//        System.out.println(empList);
//    }
}
复制代码

当然在application.properties的配置文件中 我们需要配置连接数据库的基本设置

application.properties

复制代码
#?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#???????url
spring.datasource.url=jdbc:mysql://localhost:3306/mybaits
#?????????
spring.datasource.username=root
#??
spring.datasource.password=123456789
#??mybatis?????????????
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#kaiqi tuofengmingming zidongyingshe kaiguan
mybatis.configuration.map-underscore-to-camel-case=true
复制代码

第一行说明我们需要的连接数据库驱动,同时第二行是要链接的url地址,第三行是连接数据库的地址,第四行是连接数据库的密码,第五行是配置是现在控制台打印出运行与数据库连接的日志信息,第六行是配置出开启驼峰命名自动映射的开关,此开关的作用是可以在搜查元素时,当java代码中的变量名和数据库表中不一致时,且java代码的变量是驼峰命名,数据库中是下划线命名时,自动匹配相应信息。

标签:10.24,id,emp,time,import,public,Emp
From: https://www.cnblogs.com/gjsgjs/p/17912168.html

相关文章

  • 10.24随笔
    逻辑运算And:与同时满足两个条件的值。Select*fromempwheresal>2000andsal<3000;查询EMP表中SAL列中大于2000小于3000的值。Or:或满足其中一个条件的值Select*fromempwheresal>2000orcomm>500;查询emp表中SAL大于2000或COMM大于500......
  • 10.24
    今日学习内容<%@pageimport="java.sql.PreparedStatement"%><%@pageimport="java.sql.*"%><%@pageimport="java.sql.DriverManager"%><%--CreatedbyIntelliJIDEA.TochangethistemplateuseFile|Settings......
  • 大二快乐日记10.24
    3.@WebServlet实现多重映射Servlet3.0增加了对@WebServlet注解的支持,我们可以在urlPatterns属性中,以字符串数组的形式指定一组映射规则来实现Servlet的多重映射。以servletDemo为例,在@WebServlet注解的urlPatterns属性中添加一组虚拟路径,代码如下。纯文本复制pac......
  • Linux第四章文件权限 2023.10.24
    1、UGO设置文件属性与权限chown:修改文件属主,属性chgrp:修改文件属组chmod:修改文件权限 用法例如(1)chownqfedufile2;chownqfedu02.linuxfile2(2)chgrplinux02file2(3)  1、chmodu+xfile  2、chmodu=rwxfile  3、chmod721file2、基本权限ACL(1)使用get......
  • 10.24
    跟着模板敲代码(1)项目的架构 Dao为数据持久层,用于实现数据库的增删改查entity为javabean用于封装数据库中的对象servlet为前端数据的处理层jsp为前端页面现在来一个个实现 BaseDao用于链接mysql数据库publicclassBaseDao{static{try{C......
  • 每日总结10.24
    今天是一个充实的学习日,我早上开始了算法与数据结构的课程,这门课程涵盖了许多重要的计算机科学概念。今天,我们深入研究了树和生成树的概念,这是算法和数据结构中的关键主题。学习了树的基本结构和性质,以及如何使用树来解决各种计算问题。我还学到了一些巧妙的解题方法,这些方法在算......
  • 「Log」2023.10.24 小记
    序幕/尾声昨天跑了\(1000m\),晚上享受到了优质睡眠。虽说肌肉有点疼,但无压力起床,状态拉满。下楼之后感觉没想象中那么冷,大抵跟昨天莫名其妙的霾有关系。附近在装修,到处都是尘土,但天还是很蓝。\(\text{6:50}\):慵懒到校,整整博客,今天准备写写猪国杀。\(\text{7:30}\):模拟赛开题......
  • 10.24
    今天学习了使用mybatis通过注解的方式实现对数据库最基本的增删改查定义了一个Emp的类对象Emp.javapackagecom.itheima.mybatisdatabaseexample.pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importjava.time.LocalDat......
  • 2023.10.24——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.mybatis明日计划:学习......
  • 23.10.24(jsp下拉框添加默认值)
    通过查阅网上资料,得到jsp下拉框默认值的设置方式:<selectname="zy"id="zy"required><optionvalue="0"></option><optionvalue="信息工程"<%=selectValue.equals("信息工程")?"selected&......