IDEA项⽬使⽤SpringBoot+MyBatis-Plus的⽅法步骤如下:
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&acle</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.p</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("batitsplus.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 实体类中的成员名称和表中字段名称不⼀致
总结
到此这篇关于IDEA项⽬使⽤SpringBoot+MyBatis-Plus的⽅法的⽂章就介绍到这了,更多相关idea使⽤SpringBoot+MyBatis-Plus内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
>springboot实现aop

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。