org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.baomidou
mybatis-plus-boot-starter
3.1.2
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter
mysql
mysql-connector-java
5.1.47
org.projectlombok
lombok
true
1.18.4
application.yml配置文件
server:
port: 8000
spring:
application:
name: goods
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/test?serverTimezone=UTC
username: root
password: root
mybatis-plus:
configuration:
map-underscore-to-camel-case: false #自动驼峰命名
type-enums-package: com.eunm //枚举
mapper-locations: classpath*:mapper/*.xml #配置文件地址
type-aliases-package: com.domain
模块结构
domain
@Data
@TableName("person") //表名
public class Person {
@TableId(type = IdType.AUTO) //主键
private Integer id;
private String name;
private Integer age;
private sex person_sex;
}
dao
public interface PersonMapper extends baseMapper{ public List selectAll(); }
继承baseMapper可以使用一些简单的基础方法
public enum sex implements IEnum启动类{ MAN(1,"男"), WOMAN(2,"女"); private Integer value; private String sex; sex(int value, String sex) { this.value=value; this.sex=sex; } @Override public Integer getValue() { return this.value; } @Override public String toString() { return this.sex; } }
@SpringBootApplication
@MapperScan("com.dao")
public class PlusApplication {
public static void main(String[] args) {
SpringApplication.run(PlusApplication.class,args);
}
}
PersonMapper.xml自定义的查询方法
测试//全类名
@SpringBootTest(classes = PlusApplication.class)
@RunWith(SpringRunner.class)
public class PersonServiceTest {
@Autowired
private PersonMapper personMapper;
@Test
public void selectById(){
Person person = personMapper.selectById(1);
System.out.println(person);
}
@Test
public void save(){
Person person = new Person();
person.setName("aa");
person.setAge(15);
person.setPerson_sex(sex.MAN);
int insert = personMapper.insert(person);
}
@Test
public void selectAll(){
List people = personMapper.selectAll();
System.out.println(people);
}
}
selectById
selectAll
save



