1:创建springboot的web项目。
2:加入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3:数据库表、实体类
package com.dongmu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
int id ;
String name;
String ped;
}
4:创建dao接口和mapper文件
package com.dongmu.dao;
import com.dongmu.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserDao {
public User selectUserById(@Param("userId") int id);
}
@Mapper:作用是告诉mybatis创建这个接口的实现类对象
<?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.dongmu.dao.UserDao">
<select id="selectUserById" resultType="com.dongmu.pojo.User">
select *
from User
where id = #{userId}
</select>
</mapper>
5:处理静态资源问题,链接数据库的配置文件
<!--加入 resource 插件--> <resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
server.port=8888
server.servlet.context-path=/orm
#配置链接数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dongmu?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
6:创建service、controller
package com.dongmu.service;
import com.dongmu.dao.UserDao;
import com.dongmu.pojo.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
UserDao userDao;
@Override
public User queryUser(int id) {
return userDao.selectUserById(id);
}
}
package com.dongmu.controller;
import com.dongmu.pojo.User;
import com.dongmu.service.UserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class UserController {
@Resource
private UserServiceImpl userService;
@RequestMapping("/user/getUser")
@ResponseBody
public String queryUser(@Param("id") int id){
User user = userService.queryUser(id);
System.out.println(user);
return user.toString();
}
}
查询结果
总结:@Mapper:放在每个mapper文件的上面,告诉mybatis创建接口的实现类。
可以使用如下的方式解决。
主类上添加注解包扫描:@MapperScan("com.dongmu.dao")
1.去掉 StudentMapper 接口的上面的@Mapper 注解
2.在主类上面加入 @MapperScan()
我们一般会把mapper文件放在resources的子目录mapper里面,所以再在配置文件中指定mapper文件的位置mybatis.mapper-locations=classpath:mapper/*.xml
这个时候要注意mapper文件是否编译进入了,注意静态资源问题。
配置日志:
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
7:涉及到数据库,一定会有事务
SpringBoot中使用事务: 上面的两种方式都可以。
1)在业务方法的上面加入@Transactional , 加入注解后,方法有事务功能了。
2)显式地在主启动类的上面,加入@EnableTransactionManager