内容简介:SpringBoot MySQL MyBatis jdk1.8 Maven首先我们先创建一个SpringBoot 项目。数据库连接配置 1 2 3 4 5 ##数据库连接配置(部署到哪台,对应的ip需修改) spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?connectTimeout=1000&useSSL=false&useUnicode=true&characterEncoding=UTF-8 spring.datasource.us
本例所用环境:
SpringBoot MySQL MyBatis jdk1.8 Maven
首先我们先创建一个SpringBoot 项目。
数据库连接配置 1 2 3 4 5 ##数据库连接配置(部署到哪台,对应的ip需修改) spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?connectTimeout=1000&useSSL=false&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver=com.mysql.jdbc.Driver 数据库中的数据
环境配好之后,下面分别介绍一下通过注解或者通过xml映射的形式这两种方法来使用MyBatis。
通过xml映射的形式 测试Bean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package com.example.demo.model; public class User { private int id; private String name; private String sex; private int age; public User() { } public User(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } public User(int id, String name, String sex, int age) { this.id = id; this.name = name; this.sex = sex; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
XML形式的具体操作 将mapper定义为接口,只定义方法。具体的实现在同名的xml文件中。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.example.demo.mapper; import com.example.demo.model.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface UserMapper { User getByName(@Param("name") String name); boolean insert(User user); boolean update(@Param("name") String name, @Param("sex") String sex, @Param("age") int age); void delete(@Param("name") String name); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Service服务 1 2 3 4 5 6 7 8 9 10 11 12 13 package com.example.demo.service; import com.example.demo.model.User; public interface UserService { User getUserByName(String name); boolean addUser(User user); boolean updateUser(String name, String sex, int age); void deleteUser(String name); } Service服务的实现类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.example.demo.service.impl; import com.example.demo.mapper.UserMapper; import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService{ @Autowired UserMapper userMapper; @Override public User getUserByName(String name) { User user = userMapper.getByName(name); if (null != user){ return user; } return null; } @Override public boolean addUser(User user) { return userMapper.insert(user); } @Override public boolean updateUser(String name, String sex, int age) { return userMapper.update(name, sex, age); } @Override public void deleteUser(String name) { userMapper.delete(name); } } 测试接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.example.demo.controller; import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired UserService userService; @RequestMapping(value = "/index", method = RequestMethod.GET) public String index(){ User user = userService.getUserByName("gyl"); return user.getName()+"--"+user.getSex()+"--"+user.getAge(); } } 如果一切顺利,即将输入localhost:8080/index 你将看到如下内容
通过注解的方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.example.demo.mapper; import com.example.demo.model.User; import org.apache.ibatis.annotations.*; @Mapper public interface UserMapper { @Select("select * from TB_USER where NAME = #{name}") User getByName(@Param("name") String name); @Insert("insert into TB_USER(NAME, SEX, AGE) values(#{name}, #{sex}, #{age})") boolean insert(User user); @Update("update TB_USER set SEX=#{sex}, AGE=#{age} where NAME=#{name}") boolean update(@Param("name") String name, @Param("sex") String sex, @Param("age") int age); @Delete("delete from TB_USER where NAME = #{name}") void delete(@Param("name") String name); } 如果一切顺利,即将输入localhost:8080/index 你将看到如下内容
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 利用系统缓存提高PostgreSQL操作效率
- 利用SACL审核文件操作思路分享
- 利用 Sequelize 来操作数据库
- 利用 puppeteer 来录制网页操作导出 GIF 动图
- golang: 利用unsafe操作未导出变量-Pointer使用
- 利用python 更新ssh 远程代码 操作远程服务器的实现代码
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Social Web Applications
Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99
Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!
SHA 加密
SHA 加密工具
RGB CMYK 转换工具
RGB CMYK 互转工具