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