JdbcTemplate其实用起来很方便,mybatis用得人也不少。
mybatis运用分为注解版和非注解版。
前提
1,导入maven包,pom.xml,其他的跟jdbctemplate一样。
<!-- Spring-Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2,数据库配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_user
username: root
password: root
注解版
UserMapper.java:
package com.cx.springbootdemo.mapper;
import com.cx.springbootdemo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
// @Mapper 这里可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
public interface UserMapper {
@Select("SELECT * FROM tb_user WHERE id = #{id}")
User getUserById(Integer id);
@Select("SELECT * FROM tb_user")
public List<User> getUserList();
@Insert("insert into tb_user(username, age, ctm) values(#{username}, #{age}, now())")
public int add(User user);
@Update("UPDATE tb_user SET username = #{user.username} , age = #{user.age} WHERE id = #{id}")
public int update(@Param("id") Integer id, @Param("user") User user);
@Delete("DELETE from tb_user where id = #{id} ")
public int delete(Integer id);
}
UserMapperService.java接口:
package com.cx.springbootdemo.service;
import com.cx.springbootdemo.pojo.User;
import java.util.List;
public interface UserMapperService {
User getUserById(Integer id);
public List<User> getUserList();
public int add(User user);
public int update(Integer id, User user);
public int delete(Integer id);
}
UserServiceMapperImpl.java:
package com.cx.springbootdemo.service;
import com.cx.springbootdemo.mapper.UserMapper;
import com.cx.springbootdemo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName cn.saytime.service.impl.UserServiceImpl
* @Description
*/
@Service
public class UserServiceMapperImpl implements UserMapperService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
@Override
public int add(User user) {
return userMapper.add(user);
}
@Override
public int update(Integer id, User user) {
return userMapper.update(id, user);
}
@Override
public int delete(Integer id) {
return userMapper.delete(id);
}
}
SpringbootdemoApplication.java加上一个扫描mapper的注解:
package com.cx.springbootdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.cx.springbootdemo.mapper")
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
Mybatis 注解使用 http://www.mybatis.org/mybatis-3/zh/java-api.html。
改下controller:
用Postman测试,成功。
非注解版
1,加入配置文件:
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
在resources下新建mybatis-config.xml和mapper文件夹。
mybatis-config.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
</configuration>
mapper文件夹下新建UserMapper.xml:
<?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.cx.springbootdemo.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.cx.springbootdemo.pojo.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="ctm" property="ctm" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List" >
id, username, age, ctm
</sql>
<select id="getUserList" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
</select>
<select id="getUserById" parameterType="java.lang.Integer" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
WHERE id = #{id}
</select>
<insert id="add" parameterType="com.cx.springbootdemo.pojo.User" >
INSERT INTO
tb_user
(username,age,ctm)
VALUES
(#{username}, #{age}, now())
</insert>
<update id="update" parameterType="java.util.Map" >
UPDATE
tb_user
SET
username = #{user.username},age = #{user.age}
WHERE
id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Integer" >
DELETE FROM
tb_user
WHERE
id = #{id}
</delete>
</mapper>
在com.cx.springbootdemo.mapper包下新建UserMapper.java类:
package com.cx.springbootdemo.mapper;
import com.cx.springbootdemo.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
User getUserById(Integer id);
public List<User> getUserList();
public int add(User user);
public int update(@Param("id") Integer id, @Param("user") User user);
public int delete(Integer id);
}
controller跟上面的注解版不变。
使用postman测试通过。