最近接触到springboot,用来初始化项目非常方便,于是拿来初始化了一下mybatis,结果发现,网上大部分的文章都是用注解方法进行mapper配置。
大家都知道,mybatis的一个特点就是所有的sql都在xml里,方便管理,还可以使用动态sql。
于是我开始尝试使用xml配置mapper,过程中遇到了一点点问题,现记录。
1.选择spring init
2.输入信息,第二个是项目名
3.搜索添加要用的依赖,这里注意右边,我选择了三个,web,mybatis,postgresql(也可以选择其他数据库)
4.按照红框添加包和文件
package com.u3coding.mybatistest.model; public class Users { private String userid; private String psw; private String email; private String name; private int id; public Users(int id,String userid, String name, String psw, String email) { this.id = id; this.userid = userid; this.name = name; this.psw = psw; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package com.u3coding.mybatistest.mapper; package com.u3coding.demo.mapper; import com.u3coding.demo.entity.Users; import java.util.List; public interface UsersMapper { List<Users> findAll(); Users findOne(); }
<?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.u3coding.demo.mapper.UsersMapper">//注意这里的地址 <select id="findAll" resultType="com.u3coding.demo.entity.Users"> select * from users </select> <select id="findOne" resultType="com.u3coding.demo.entity.Users"> select * from users where id = 1 </select> </mapper>
package com.u3coding.demo.controller; import com.u3coding.demo.entity.Users; import com.u3coding.demo.mapper.UsersMapper; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/reset/users") public class UsersController { private UsersMapper usersMapper; public UsersController(UsersMapper mapper){ this.usersMapper = mapper; } @GetMapping("/all") public List<Users> getAll(){ return usersMapper.findAll(); } @GetMapping("/one") public Users getone(){ return usersMapper.findOne(); } }
server.port=9000 //服务器端口 spring.datasource.url=jdbc:postgresql://localhost:5432/redb //本地数据库地址,5432是我安装postgresql的端口 spring.datasource.username=u3 //数据库用户名 spring.datasource.password=pw //数据库密码 spring.datasource.testWhileIdle=true //当连接空闲时,是否执行连接测试 spring.datasource.validationQuery=SELECT 1 //指定获取连接时连接校验的sql查询语句. mybatis.mapper-locations=classpath:mappers/*.xml //指定mapperxml所在的位置,要用/分割
package com.u3coding.demo; import com.u3coding.demo.entity.Users; import org.apache.ibatis.type.MappedTypes; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MappedTypes(Users.class) @MapperScan("com.u3coding.demo.mapper")//mapper接口的地址 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
运行我们刚刚编写好的程序入口,再在浏览器访问,我们就可以看到返回了
最后,你可以在 这里 里找到这个项目的源码