一、数据表
1、创建User表结构如下
二、新建spring工程
1、注意选择java 8其他默认,下一步
2、勾选常用的依赖,不足后期可以在POM.xml中手动补充,然后通过maven更新依赖。
3、工程目录结构如下,根据如下工程目录结构建立文件夹
三、参考代码
1、User实体类如下
public class User {
private int id;
private String userName;
private String passWord;
private String realName;
//省略 getter and setter
}
2、Mapper
@Repository
public interface UserMapper {
User SelUserById(int id);
}
3、Mapper.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.example.demo.mapper.UserMapper"> <!--namespace 对应的UserMapper接口位置-->
<select id="SelUserById" parameterType="Integer" resultType="com.example.demo.entity.User"> <!-- resultType对应实体类路径-->
select *from user where id=#{id}
</select>
</mapper>
4、UserService
public interface UserService {
User SelUser(int id);
}
5、UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
//@Autowired 方式2也可以
@Resource
private UserMapper userMapper;
public User SelUser(int id){
return userMapper.SelUserById(id);
}
}
6、UserController类
@RestController
@RequestMapping("/test")
public class UserController {
//@Autowired 方式2也可以
@Resource
UserService userService;
@RequestMapping(value = "/getUser")
@ResponseBody
public User getUserService(HttpServletRequest request) {
return userService.SelUser(Integer.valueOf(request.getParameter("id")));
}
@ResponseBody
@RequestMapping("/getUser2/{id}")
public User GetUser(@PathVariable("id") int id){
return userService.SelUser(Integer.valueOf(id));
}
}
7、启动类
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
8、配置文件application.properties
建议首次使用.properties格式配置文件,格式不容易出错
yml格式文件需特别注意,":"为英文,其后要留空格 ,注意所有符号均为英文格式,否则很难查找错误。
替换如下括号位置的中文内容
server.port=8080
spring.datasource.url= jdbc:mysql://localhost:3306/数据库名(需要替换)
spring.datasource.username= root
spring.datasource.password=密码 (需要替换)
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package= com.example.demo.entity
mybatis.mapper-locations = classpath:mappers/*.xml (mapper.xml路径位置)
或者application.yml
server:
port: 8080
spring:
datasource:
username: root
password: 密码 (需要替换)
url: jdbc:mysql://localhost:3306/ 数据库名(需要替换)
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.example.springmybatis2.entity
mapper-locations: classpath:mapper/*Mapper.xml (mapper.xml路径位置)
四、代码调试及常见错误
1、正常启动访问效果
2、常见错误汇总
1)错误1
APPLICATION FAILED TO START
原因:启动函数部分
参考:@MapperScan(“com.example.demo.mapper”) 有没有@MapperScan,并含有mapper路径
2)错误2
Error creating bean with name ‘xxController’: Unsatisfied dependency expressed through field ‘userService’;nested exception is org.springframework.beans.factory.
原因:xxMapper.xml文件有问题,
参考:特别注意文件头部有无问题,文件头部拼写正确才会有如下SQL部分的背景颜色的提示
3)错误3 也是最为常见的问题
ERROR:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): …] with root cause
原因:xxMapper.xml或properties文件里的几处路径拼写问题
追加笔记1当实体类和数据库字段名称不一致时候的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.springbootmybatis1.dao.UserMapper"> <!--namespace 对应的UserMapper接口位置-->
<!--方式一-->
<!--<resultMap id="userResultMap" type="com.springbootmybatis1.entity.User">
<id property="id2" column="id"/>
<result property="userName2" column="userName"/>
<result property="passWord2" column="passWord"/>
<result property="realName2" column="realName"/>
</resultMap>
<select id="SelUserById" parameterType="Integer" resultMap="userResultMap"> <!– resultType对应实体类路径–>
select *from user where id=#{id2}
</select>-->
<!--方式二-->
<select id="SelUserById" parameterType="Integer" resultType="com.springbootmybatis1.entity.User">
select `id` `id2`,`userName` `userName2`,`passWord` `passWord2`, `realName` `realName2` from user where id=#{id2}
</select>
</mapper>