<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
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
@SpringBootTest
class Spring01MybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
class com.zaxxer.hikari.HikariDataSource
HikariProxyConnection@438660480 wrapping com.mysql.cj.jdbc.ConnectionImpl@3069a360
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
//这个注解表示了这是一个mybatis的mapper 类:Dao
@Mapper
@Repository
public interface UserMapper {
User QueryUserByID(int id);
int addUser(User user);
int updateUser(User user);
List<User> QueryUser();;
int deleteUser(int id);
}
<?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.wang.mapper.UserMapper">
<select id="QueryUser" parameterType="ArrayList" resultType="User">
select * from mybatis.user
</select>
<select id="QueryUserByID" resultType="User">
select * from mybatis.user
</select>
<insert id="addUser" parameterType="User">
insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="User">
update mybatis.user set name = #{name} pwd = #{pwd} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
#整合mybatis
mybatis.type-aliases-package=com.wang.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
@RestController
public class userController {
@Autowired
private UserMapper userMapper;
@GetMapping("/QueryUser")
public List<User> QueryUser(){
List<User> users = userMapper.QueryUser();
for (User user : users) {
System.out.println(user);
}
return users;
}
}
原文:https://www.cnblogs.com/wu-wang/p/14917514.html