pom.xml
文件
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
application.yml
#配置相关信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver该驱动类过时
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password: root
public class Person {
private Integer id;
private String name;
private String city;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + ''' +
", city='" + city + ''' +
'}';
}
}
方式一:纯注解开发
@Mapper //SQL映射添加@Mapper注解方便被容器识别
public interface PersonDao {
@Select("select * from person where id = #{id}")
public Person getById(Integer id);
}
方式二:xml开发
新建一个PersonMapper接口
@Mapper
public interface PersonMapper {
public List findAll();
}
resources下新建一个mapper文件夹并在里面新建 PersonMapper.xml文件
在application.yml 中添加MyBatis配置
#MyBatis配置
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件路径
type-aliases-package: com.ityun.springbootstudy.domain #定义实体类的别名
@SpringBootTest
class SpringbootStudyApplicationTests {
@Autowired
private PersonDao personDao;
@Autowired
private PersonMapper personMapper;
@Test
public void findTest() {
System.out.println(personDao.getById(1));
}
@Test
public void findAllTest(){
List list = personMapper.findAll();
System.out.println(list);
}
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦