IDEA项目使用SpringBoot+MyBatis-Plus的方法
我叫yeeking啊 人气:0步骤如下:
1.打开IDEA
2.File—>new—> project
3.选择spring initializr—>Next
4.填写Grouphe和Artifact,选择Java version: 8 ,点击next ,如图:
5.选择对应的依赖,点击Next
6.核对项目的名字是否一致,点击finish后就完成了工程的创建。
7.接下来就是pom文件的依赖包引入了(很重要!!!)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4.0-atlassian-hosted</version> </dependency> <!-- orail8n字符集 --> <dependency> <groupId>cn.easyproject</groupId> <artifactId>orai18n</artifactId> <version>11.2.0.4</version> </dependency> <!-- mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1.tmp</version> </dependency> <!-- 代码生成--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.0</version> </dependency> <!-- 代码生成模板--> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>
8.在appliaction.propertiles配置文件中写入数据库参数
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=用户 spring.datasource.password=密码 spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
9.在Java下创建对应的pojo和mapper包,并创建对应的类
10.在pojo包中新建和数据库userinfo表映射的类
@Data @KeySequence("SEQ_USER_INFO") public class UserInfo { @TableId(value = "USER_ID",type = IdType.INPUT)//在自增主键的变量加上即可 private Long userId; private String userName; @TableField(value = "USER_NINAME") //可以不写,但字段名要用小驼峰命名 private String userNiName; private String userPwd; private Date userCtime; private Integer userState; private Integer userSex; private String userEdu; private String userPro; private String userEmail; private String userTel; private Long userScore; }
11.在mapper包中创建mapper接口,并集成mybatisPlus的BaseMapper
public interface UserInfoMapper extends BaseMapper<UserInfo> { }
12.在DemoApplication的main方法中添加注解@MapperScan,使其能够扫描mapper类
,添加@SpringBootApplication注解
@MapperScan("com.mybatitsplus.demo") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
13.在测试类中:
添加注解@RunWith(SpringRunner.class)
添加注解@Resource
最后打印输出
@RunWith(SpringRunner.class) @SpringBootTest class DemoApplicationTests { @Resource private UserInfoMapper userInfoMapper; @Test public void select() { List<UserInfo> userInfos = userInfoMapper.selectList(null); userInfos.forEach(System.out::println); } }
14.右击运行测试类,就输出打印信息了。
常用注解
MyBatisPlus提供了一些注解供我们在实体类和表信息出现不对应的时候使用。通过使用注解完成逻辑上匹配。
注解名称 说明
@TableName 实体类的类名和数据库表名不一致
@TableId 实体类的主键名称和表中主键名称不一致
@TableField 实体类中的成员名称和表中字段名称不一致
总结
加载全部内容