1.创建数据库
DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie` (`id` int(255) NOT NULL AUTO_INCREMENT,`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`score` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY (`id`) USING BTREE
)
INSERT INTO `movie` VALUES (1, '美丽人生', '罗伯托·贝尼尼', '9.6', '一个快乐的传说');
INSERT INTO `movie` VALUES (2, '放牛班的春天', ' 克里斯托夫·巴拉蒂', '9.3', ' 歌声伴我心');
INSERT INTO `movie` VALUES (3, '触不可及', '奥利维埃·纳卡什 / 埃里克·托莱达诺', '9.3', '闪亮人生');
INSERT INTO `movie` VALUES (4, '三傻大闹宝莱坞', '拉吉库马尔·希拉尼', '9.2', ': 三个傻瓜');
INSERT INTO `movie` VALUES (5, '怦然心动', '罗伯·莱纳', '9.1', '梧桐树之恋');
INSERT INTO `movie` VALUES (6, '绿皮书', '彼得·法雷里', '8.9', ' 绿簿旅友');
INSERT INTO `movie` VALUES (7, '功夫', ' 周星驰', '8.8', '功夫3D');
INSERT INTO `movie` VALUES (8, '悲伤逆流成河', '郭敬明', '8.8', '逆流成河');
INSERT INTO `movie` VALUES (9, '肖申克的救赎', '弗兰克·德拉邦特', '9.8', '月黑高飞');
INSERT INTO `movie` VALUES (14, '霸王别姬', '陈凯歌', '9.6', '再见,我的妾');
INSERT INTO `movie` VALUES (15, '泰坦尼克号', ' 詹姆斯·卡梅隆', '9.4', '铁达尼号');
INSERT INTO `movie` VALUES (16, '这个杀手不太冷', '吕克·贝松', '9.4', '终极追杀令');
INSERT INTO `movie` VALUES (17, '千与千寻', '宫崎骏', '9.4', '神隐少女');
2.新建一个名为springboot-mybatis的springboot工程
3.配置yml
spring:datasource:url: jdbc:mysql:///你的数据库名?serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driverusername: 你的数据库用户名password: 你的数据库密码
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Movie {private Integer id;private String name;private String author;private String score;private String title;
}
5.创建数据层
@Mapper
public interface MovieDao {/*** 查询所有* @return*/@Select("select * from movie")public List<Movie> findAll();/*** 根据id查询*/@Select("select * from movie where id=#{id}")public Movie findById(Integer id);/*** 新增*/@Insert("insert into movie (name,author,score,title) " +"values (#{name},#{author},#{score},#{title})")public int add(Movie movie);/*** 修改*/@Update("update movie set name=#{name},author=#{author},score=#{score},title=#{title}" +"where id=#{id}")public int updateById(Movie movie);/*** 删除*/@Delete("delete from movie where id=#{id}")public int delete(Integer id);/*** 分页查询*/@Select("select * from movie limit #{current},#{size}")public List<Movie> findPage(@Param("current") Integer current,@Param("size")Integer size);/*** 条件查询*/@Select("select * from movie where name like #{name}")public List<Movie> findQuery(String name);}
6.创建服务接口类
public interface MovieService {/*** 查询所有* @return*/@Select("select * from movie")public List<Movie> findAll();/*** 根据id查询*/public Movie findById(Integer id);/*** 新增*/public int add(Movie movie);/*** 修改*/public int updateById(Movie movie);/*** 删除*/public int delete(Integer id);/*** 分页查询*/public List<Movie> findPage(Integer current, Integer size);/*** 条件查询*/public List<Movie> findQuery(String name);}
7.创建服务接口实现类
@Service
public class MovieServiceImpl implements MovieService {@Autowiredprivate MovieDao movieDao;/*** 查询所有* @return*/@Overridepublic List<Movie> findAll() {return movieDao.findAll();}/*** 根据id查询*/@Overridepublic Movie findById(Integer id) {return movieDao.findById(id);}/*** 新增*/@Overridepublic int add(Movie movie) {return movieDao.add(movie);}/*** 修改*/@Overridepublic int updateById(Movie movie) {return movieDao.updateById(movie);}/*** 删除*/@Overridepublic int delete(Integer id) {return movieDao.delete(id);}/*** 分页查询*/@Overridepublic List<Movie> findPage(Integer current, Integer size) {return movieDao.findPage(current, size);}/*** 条件查询*/@Overridepublic List<Movie> findQuery(String name) {return movieDao.findQuery(name);}
}
8.测试
@SpringBootTest
class SpringbootMybatisApplicationTests {@Autowiredprivate MovieService movieService;/*** 查询所有*/@Testpublic void testFindAll() {List<Movie> movieList = movieService.findAll();for (Movie movie : movieList) {System.out.println(movie);}}/*** 根据id查询*/@Testpublic void testFindById(){Movie movie = movieService.findById(4);System.out.println(movie);}/*** 新增*/@Testpublic void testAdd(){Movie movie=new Movie();movie.setName("看不见的客人");movie.setScore("9.2");movie.setAuthor("奥里奥尔·保罗");movie.setTitle("消失的客人");int count = movieService.add(movie);System.out.println(count);}/*** 修改*/@Testpublic void testUpdateById(){Movie movie=new Movie();movie.setId(18);movie.setName("看不见的客人");movie.setScore("9.5");movie.setAuthor("奥里奥尔·保罗");movie.setTitle("消失的客人");int count = movieService.updateById(movie);System.out.println(count);}/*** 删除*/@Testpublic void testDelete(){int count = movieService.delete(18);System.out.println(count);}/*** 分页查询*/@Testpublic void testFindPage(){List<Movie> movieList = movieService.findPage(1, 2 );for (Movie movie : movieList) {System.out.println(movie);}}/*** 条件查询*/@Testpublic void testFindQuery(){String name="悲伤";String query="%"+name+"%";List<Movie> movieList = movieService.findQuery(query);System.out.println(movieList);}}