大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
Maven引入
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
创建实体类
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> ordersList;
}
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<User>, MySqlMapper<Users> {
public Users selectByUsername(String username); //tkMapper中默认的方法不能满足当前要求可以自定义方法
}
UserMapper.xml
如果tkMybatis默认的方法不能满足当下的要求可以,将自定义的方法写入Mapper.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” >
<mapper namespace=”com.bruce.mapper.UsersDao” >
<!–namespace根据自己需要创建的的mapper的路径和名称填写–>
<resultMap id=”userMap” type=”com.bruce.entity.Users”>
<id column=”user_id” property=”userId”></id>
<result column=”username” property=”userName”></result>
<result column=”password” property=”userPassword”></result>
<result column=”nickname” property=”nickName”></result>
<result column=”realname” property=”userRealName”></result>
<result column=”user_img” property=”userImg”></result>
<result column=”user_mobile” property=”userMobile”></result>
<result column=”user_email” property=”userEmail”></result>
<result column=”user_sex” property=”userSex”></result>
<result column=”user_birth” property=”userBirth”></result>
<result column=”user_regtime” property=”userRegtime”></result>
<result column=”user_modtime” property=”userModtime”></result>
<result column=”user_age” property=”userAge”></result>
<collection property=”ordersList” ofType=”com.bruce.entity.Orders”>
<id column=”order_id” property=”orderId”></id>
<result column=”receiver_name” property=”receiverName”></result>
<result column=”receiver_mobile” property=”receiverMobile”></result>
<result column=”receiver_address” property=”receiverAddress”></result>
</collection>
</resultMap>
<select id=”selectByUsername” resultMap=”userMap”>
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
</select>
</mapper>
修改启动类的`@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> users = usersDao.selectByExample(example);
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<Orders> ordersList = ordersDao.selectByExample(example1);
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中引入
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
在build标签下面的plugins标签下加入
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!– 这里指定配置文件 –>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
把配置文件放到resources下的generator目录下(这里目录可以随意,但要跟上面pow.xml文件中的 configurationFile 中指定目录一致)
内置文件内容如下
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE generatorConfiguration
PUBLIC “-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN”
“http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd”>
<generatorConfiguration>
<!– 引入数据库连接配置 –>
<!– <properties resource=”jdbc.properties”/>–>
<context id=”Mysql” targetRuntime=”MyBatis3Simple” defaultModelType=”flat”>
<property name=”beginningDelimiter” value=”`”/>
<property name=”endingDelimiter” value=”`”/>
<!– 配置 GeneralDAO –>
<plugin type=”tk.mybatis.mapper.generator.MapperPlugin”>
<property name=”mappers” value=”com.bruce.general.GeneralDao”/>
</plugin>
<!– 配置数据库连接 –>
<jdbcConnection driverClass=”com.mysql.jdbc.Driver”
connectionURL=”jdbc:mysql://localhost:3306/fmmall”
userId=”root” password=”root”>
</jdbcConnection>
<!– 配置实体类存放路径 –>
<javaModelGenerator targetPackage=”com.bruce.entity” targetProject=”src/main/java”/>
<!– 配置 XML 存放路径 –>
<sqlMapGenerator targetPackage=”/” targetProject=”src/main/resources/mappers”/>
<!– 配置 DAO 存放路径 –>
<javaClientGenerator targetPackage=”com.bruce.mapper” targetProject=”src/main/java” type=”XMLMAPPER”/>
<!– 配置需要指定生成的数据库和表,% 代表所有表 –>
<table tableName=”%”>
<!– mysql 配置 –>
<!– <generatedKey column=”id” sqlStatement=”Mysql” identity=”true”/>–>
</table>
<!– <table tableName=”tb_roles”>–>
<!– <!– mysql 配置 –>–>
<!– <generatedKey column=”roleid” sqlStatement=”Mysql” identity=”true”/>–>
<!– </table>–>
<!– <table tableName=”tb_permissions”>–>
<!– <!– mysql 配置 –>–>
<!– <generatedKey column=”perid” sqlStatement=”Mysql” identity=”true”/>–>
<!– </table>–>
</context>
</generatorConfiguration>
执行生成
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184928.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...