标签:return pac 注解 tty stc red zone odi doctype
创建项目时勾选依赖
web依赖
lombok
mysql和mybatis
或者直接在pom.xml中导入依赖(mybatis/mysql/)
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
ps : 如果整合Druid只需要把这一步省略,将数据库连接交给Druid编写
详见:
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合mybatis
mybatis.type-aliases-package=com.yue.pojo
#此处的地址与下文第六步的地址需要一样
mybatis.mapper-locations=classpath:mapper/*.xml
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
//这个注解表示了这是一个 mybatis 的 mapper 类
@Mapper
@Repository
public interface UserMapper {
List<User> getAll();
User getUserById(int id);
int addUser(User user);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yue.dao.UserMapper">
<select id="getAll" resultType="User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="User">
select * from mybatis.user where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
</mapper>
@RestController
public class UserController {
//如果前面的mapper.xml的地址没有配错的话,这里也就顺利的使用自动装配
@Autowired
UserMapper userMapper;
@RequestMapping("/getall")
public List<User> getAll(){
List<User> all = userMapper.getAll();
return all;
}
}
成功
标签:return pac 注解 tty stc red zone odi doctype
原文地址:https://www.cnblogs.com/yue1234/p/12651289.html