1.建立springboot maven项目
2.建立数据库需要用的表
3.建立pom文件中相应的依赖
4.建立application.properties文件
# 对应的jsp文件路径前缀 src/main/webapp
spring.mvc.view.prefix=/WEB-INF/jsp/
# 对应的jsp文件后缀
spring.mvc.view.suffix=.jsp
# 数据库的配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
# 配置需要读取的实体类,这里统一做包的配置
# mybatis.config= classpath:mybatis-config.xml
mybatis.typeAliasesPackage=com.tim.springboot.entity
mybatis.mapperLocations=classpath:mappers/*.xml
5.写相应的类
1).建立实体类User.java
2).建立Mapper.java 接口
package com.tim.springboot.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.tim.springboot.entity.User;
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
注意:一定要在mapper上面加上@Mapper注解,如果不加启动会出现
Description:
Field userMapper in com.tim.springboot.controller.MybatisController required a bean of type 'com.tim.springboot.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.tim.springboot.mapper.UserMapper' in your configuration.
这样的错误
如果不加@Mapper的话,启动类必须这样写
@ComponentScan(basePackages="com.tim.springboot.controller")
@MapperScan(basePackages="com.tim.springboot.mapper")
@SpringBootApplication
才不会报以上的错误
3).建立UserMapper.xml配置文件
id, name, age
delete from user
where id = #{id,jdbcType=INTEGER}
insert into user (id, name, age
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
)
insert into user
id,
name,
age,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
update user
name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
update user
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
4).建立MybatisController.java文件 用于测试
package com.tim.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tim.springboot.entity.User;
import com.tim.springboot.mapper.UserMapper;
@RestController
public class MybatisController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/mybatis")
public User getUser() {
User user = userMapper.selectByPrimaryKey(1);
System.out.println("成功了!");
return user;
}
}
5).建立启动类:
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationTest {
public static void main( String[] args ){
SpringApplication.run(ApplicationTest.class, args);
}
}
6.启动项目:
右击项目 点击 run As ==> Maven clear ====> maven install
右击项目 点击 run As ===> java application ===》 在浏览器输入:localhost:8080/mybatis
7.结果显示:
项目总的目录结构: