idea+jdk8+springboot+mysql+mybatis
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8
COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1
CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
这个表结构很简单,也就是三个字段id、name、age。并以id为主键且递增。
选择File->new->Project->Spring Initializr
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.44version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
OK,我们只需要加上这些依赖即可。在我们的pom.xml文件。
我们只需要把application.properties文件改为yml格式即可。此时添加相关配置
#配置服务器信息
server:
port: 8080
spring:
#mysql数据库相关配置
datasource:
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#mybatis依赖
mybatis:
type-aliases-package: com.homyit.mybatis.dao
这里的配置有点多,不过还是一个最基本的配置都在这。
public class Person {
private int id ;
private String name;
private int age;
public Person() {
}
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
//getter和setter方法
//toString方法
}
这个类是和我们数据库中的Person类一一对应的。
在这个类中,我们实现基本的增删改查功能接口:
@Mapper
public interface PersonMapper {
//增加一个Person
@Insert("insert into person(id,name,age)values(#{id},#{name},#{age})")
int insert(Person person);
//删除一个Person
@Delete("delete from person where id = #{id}")
int deleteByPrimaryKey(Integer id);
//更改一个Person
@Update("update person set name =#{name},age=#{age} where id=#{id}")
int updateByPrimaryKey(Integer id);
//查询一个Person
@Select("select id,name ,age from person where id = #{id}")
Person selectByPrimaryKey(Integer id);
//查询所有的Person
@Select("select id,name,age from person")
List<Person> selectAllPerson();
}
这就是最基本的一个增删改查操作的接口。
public interface PersonService {
//增加一个Person
int insertPerson(Person person);
//删除一个Person
int deleteByPersonId(Integer id);
//更改一个Person
int updateByPersonId(Person record);
//查询一个Person
Person selectByPersonId(Integer id);
//查询所有的Person
List<Person> selectAllPerson();
}
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper;
@Override
public int insertPerson(Person person) {
return personMapper.insert(person);
}
@Override
public int deleteByPersonId(Integer id) {
return personMapper.deleteByPrimaryKey(id);
}
@Override
public int updateByPersonId(Person record) {
return personMapper.updateByPrimaryKey(record);
}
@Override
public Person selectByPersonId(Integer id) {
return personMapper.selectByPrimaryKey(id);
}
@Override
public List<Person> selectAllPerson() {
return personMapper.selectAllPerson();
}
}
@RestController
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping(value = "/add")
public String students () {
Person person = new Person();
person.setId(1);
person.setName("Homyit Studio");
person.setAge(18);
int result = personService.insertPerson(person);
System.out.println("插入的结果是:"+result);
return result+"";
}
@RequestMapping(value = "/findAll")
public String findAll () {
List<Person> people = personService.selectAllPerson();
people.stream().forEach(System.out.println);
return people.toString()+"";
}
}
@SpringBootApplication
@MapperScan("com.homyit.mybatis.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}