Maven引入
创建实体类(字段类型一定要是对象类型,不能是基础类型:要写Integer不能写int,要写Long不能写long)
User.java类
package com.bruce.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.Column;
import javax.persistence.Id;
import java.util.Date;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Users {
@Id
private Integer userId;
@Column(name = "username")
private String userName;
@Column(name = "password")
private String userPassword;
@Column(name = "nickname")
private String nickName;
@Column(name = "realname")
private String userRealName;
private String userImg;
private Integer userAge;
private String userMobile;
private String userEmail;
private Boolean userSex;
private Date userBirth;
private Date userRegtime;
private Date userModtime;
private List
}
Orders.java 类
package com.bruce.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.Id;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Orders {
@Id
private String orderId;
private Long userId;
private String receiverName;
private String receiverMobile;
private String receiverAddress;
}
创建DAO接口
tkMapper已经完成了对单表的通用操作的封装,封装在Mapper接口和MySqlMapper接口;因此如果我们要完成对单表的操作,只需自定义DAO接口继承Mapper接口和MySqlMapper接口
@Repository
public interface UserDAO extends Mapper
public Users selectByUsername(String username); //tkMapper中默认的方法不能满足当前要求可以自定义方法
}
UserMapper.xml
如果tkMybatis默认的方法不能满足当下的要求可以,将自定义的方法写入Mapper.xml的配置文件中
select
u.user_id,
u.username,
u.password,
u.nickname,
u.realname,
u.user_img,
u.user_mobile,
u.user_email,
u.user_sex,
u.user_birth,
u.user_regtime,
u.user_modtime,
u.user_age,
o.order_id,
o.receiver_name,
o.receiver_mobile,
o.receiver_address
from users u inner join orders o
on u.user_id = o.user_id
修改启动类的`@MapperScan`注解的包
配置的MapperScan不是mybatis的而是`tk.mybatis.spring.annotation.MapperScan`
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.bruce.mapper")
public class TkmapperDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TkmapperDemoApplication.class, args);
}
}
Junit4 测试定义的方法
package com.bruce.mapper;
import com.bruce.TkmybatisApplication;
import com.bruce.entity.Orders;
import com.bruce.entity.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmybatisApplication.class)
public class UserDaoTest {
@Autowired
private UsersDao usersDao;
@Autowired
private OrdersDao ordersDao;
@Test
public void testSelect() {
Example example = new Example(Users.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("userName", "bruce");
List
Users user1 = users.get(0);
System.out.println(user1);
Example example1 = new Example(Orders.class);
Example.Criteria criteria1 = example1.createCriteria();
criteria1.andEqualTo("userId", user1.getUserId());
List
user1.setOrdersList(ordersList);
System.out.println(user1);
System.out.println(user1.getOrdersList());
}
@Test
public void testSelect2(){
Users bruce = usersDao.selectByUsername("bruce");
System.out.println(bruce);
}
}
逆向工程操作(用于生成代码)
maven引入
在dependencies中引入
在build标签下面的plugins标签下加入
把配置文件放到resources下的generator目录下(这里目录可以随意,但要跟上面pow.xml文件中的 configurationFile 中指定目录一致)
内置文件内容如下
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="jdbc:mysql://localhost:3306/fmmall"
userId="root" password="root">