mybatis编写方式:通过mapper.xml方式编写sql操作mysql数据库。
1、创建Springboot2.0 项目
1.1 new Project
1.2 创建springboot
1.3 Maven构建项目
1.4 选择2.0 以上的版本,勾选Web
1.5 在SQL上勾选Mysql 和Mybatis
1.6 Next,选择项目位置目录
1.7 创建项目目录结构图
2、配置集成mysql
2.1 application.properties配置属性
#mysql: spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ds spring.datasource.username=root spring.datasource.password=xiaonengweiwu 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
application.properties如下所示:
如果你想使用application.yml文件的话,更改下他的后缀名,改为yml就好,属性配置如图,类似于一个树形图,记住冒号后面需要有空格(‘:’ 需要有空格,才能显示颜色),而且要有层级关系。
3、集成Mybatis的xml方式
3.1 项目目录构建
mappings.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> <typeAlias alias="Integer" type="java.lang.Integer" /> <typeAlias alias="Long" type="java.lang.Long" /> <typeAlias alias="HashMap" type="java.util.HashMap" /> <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" /> <typeAlias alias="ArrayList" type="java.util.ArrayList" /> <typeAlias alias="LinkedList" type="java.util.LinkedList" /> </typeAliases> </configuration>
3.2 创建表结构
CREATE TABLE `user` ( `id` varchar(50) DEFAULT NULL, `username` varchar(30) NOT NULL, `password` varchar(40) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1
3.3 实体类
public class User implements Serializable { private String id; private String username; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
3.4 创建UserMapper
@Mapper public interface UserMapper { User getUser(String id); void delUser(String id); void addUser(User user); void updateUser(User user); }
3.5 创建UserService
@Service("userService") public class UserService { @Resource UserMapper userMapper; public User getUser(String id){ return userMapper.getUser(id); } @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.MANDATORY,readOnly = true) public void addUser(User user){ userMapper.addUser(user); } public void delUser(String id){ userMapper.delUser(id); } public void updateUser(User user){ updateUser(user); } }
这样,我们创建UserService成功,但是一般情况下,我们引入的userMapper出现红色下划线,是因为配置的原因,这里也能够正常运行,IDEA在编辑时提示could not autowire, 解决办法 File-Project Structure 页面 Facets下删掉 Spring(直接右键Delete)
3.6 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.springboot2.mjt01.mapper.UserMapper"> <resultMap id="gainUsers" type="com.springboot2.mjt01.entity.User"> <id property="id" column="id" jdbcType="VARCHAR" /> <result property="username" column="username" jdbcType="VARCHAR" /> <result property="password" column="password" jdbcType="VARCHAR" /> </resultMap> <select id="getUser" parameterType="string" resultType="com.springboot2.mjt01.entity.User"> select * from user where <if test="id != null"> id = #{id} </if> </select> <insert id="addUser" parameterType="com.springboot2.mjt01.entity.User"> INSERT INTO USER (id,username,password) VALUES (#{id},#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR}) </insert> <insert id="addUsers" parameterType="java.util.List"> INSERT INTO USER (id,username,password) VALUES <foreach collection="list" item="user" index="index" separator="" open="" close=""> (#{id},#{username},#{password}) </foreach> </insert> <delete id="delUser" parameterType="string"> DELETE FROM USER WHERE id = #{id} </delete> <update id="updateUser" parameterType="com.springboot2.mjt01.entity.User"> UPDATE USER SET id = #{id}, username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} </update> </mapper>
3.7 创建UserController
@Controller public class UserController { @Autowired UserService userService @GetMapping(value = "/mjt/user/getUser",produces = "application/json") @ResponseBody public String getUser(){ String id = "3fa444d0-c149-48b1-bc3e-cbe1bdcda300"; User user = userService.getUser(id); System.out.println(user.getPassword()); System.out.println(user.getUsername()); return user.getPassword()+user.getUsername(); } }
3.8 application.properties配置属性
#mybatis: mybatis.mapper-Locations=classpath:mapper/entity/*.xml mybatis.type-aliases-package=com.com.springboot2.mjt01.entity mybatis.config=classpath:mapper/mappings.xml
运行成功截图:
三种方法测试:
1、浏览器,不过只能支持Get方法 2、PostMan工具 3、Curl工具,你可以在gitBash上面也可以直接使用
来源:oschina
链接:https://my.oschina.net/u/3209213/blog/1834269