赞
踩
此项目代码已上传至github:https://github.com/snowlavenderlove/springMybatisProject
1.新建数据库spring_mybatis
2.新建表user
表中字段id,name,password,id为主键并自增
3.进入网址https://start.spring.io/新建springboot项目,此处可参考博文https://mp.csdn.net/postedit/90669242
4.将生成的zip压缩包解压,打开Eclipse->Import->Maven->Existing Maven Project
5.使用generator-mybatis自动生成数据库对应类相关代码,此处可参考 https://mp.csdn.net/postedit/90692784,此处不再赘述
6.将第五步生成的包复制到springMybatisProject中,如图
7.编辑pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.2.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.xue</groupId>
- <artifactId>springMybatisProject</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springMybatisProject</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
-
-
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- <version>5.1.28</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.27</version>
- </dependency>
-
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.2</version>
- </dependency>
-
-
-
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>

8.编辑application.properties
- #mysql
- spring.datasource.url=jdbc:mysql://localhost:3306/spring_mybatis
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.username=root
- spring.datasource.password=123456
- #druid datasource
- spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
-
- #mybatis
- mybatis.type-aliases-package=com.xue.reprository.dao
- mybatis.mapper-locations=classpath*:com/xue/repository/mapper/*.xml
-
9.在com.xue下创建controller包,并新建SysLoginController类
10.在com.xue下创建service包,在service包下创建sys包,并新建UserService接口,同时在sys包下创建新包Impl,在Impl下新建实现类UserServiceImpl如图:
11.最终项目结构如图:
12.编辑UserService.java
- package com.xue.service.sys;
-
- import java.util.List;
-
- import com.xue.entity.model.User;
-
- public interface UserService {
-
- //增
- public int addUser(User user);
- //删
- public int deleteUser(int id);
- //改
- public int updateUser(User user);
- //查
- public List<User> getUser();
-
-
-
- }

13.编辑UserServiceImpl.java
- package com.xue.service.sys.Impl;
-
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import com.xue.entity.model.User;
- import com.xue.repository.dao.UserMapper;
- import com.xue.service.sys.UserService;
- @Service
- public class UserServiceImpl implements UserService {
-
- @Autowired
- private UserMapper dao;
-
- //增
- @Override
- public int addUser(User user) {
-
- int result = 0;
- try{
- result = dao.insert(user);
- }catch(Exception e){
-
- e.printStackTrace();
- }
-
- return result;
- }
-
-
- //删
- @Override
- public int deleteUser(int id) {
-
- int result =0;
- try {
- result = dao.deleteByPrimaryKey(id);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- return result;
- }
- //改
- @Override
- public int updateUser(User user) {
- int result = 0;
-
- try {
- result = dao.updateByPrimaryKey(user);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- return result;
- }
-
- //查
- @Override
- public List<User> getUser() {
-
- return dao.selectAllInfo();
- }
-
-
- }

14.编辑UserMapper.java,在最后添加
15.编辑UserMapper.xml,在最后添加
16. 编辑sysLoginController.java
- package com.xue.controller;
-
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import com.xue.entity.model.User;
- import com.xue.service.sys.UserService;
-
- @RestController
- @RequestMapping("/sys/acct")
- public class SysLoginController {
-
- @Autowired
- private UserService userService;
-
- //增
- @RequestMapping("/addUser")
- public int addUser(){
-
- //向数据库添加两条记录
-
- User user = new User();
- user.setName("zhang san ");
- user.setPassword("123456");
-
- User user1 = new User();
- user1.setName("li si");
- user1.setPassword("123456");
-
- int result = userService.addUser(user);
-
- int result1 = userService.addUser(user1);
-
- return result & result1;
- }
- //删
- @RequestMapping("/deleteUser")
- public int deleteUser(){
-
- //删除数据中中第一条记录
- int result = userService.deleteUser(1);
-
- return result;
- }
-
- @RequestMapping("/updataUser")
- public int updateUser(){
-
- //修改数据库中第二条记录
-
- User user = new User();
- user.setId(2);
- user.setName("chen er");
- user.setPassword("123");
- int result = userService.updateUser(user);
- return result;
- }
-
-
- @RequestMapping("/showUser")
- public List<User> getUserInfo(){
-
- //查询数据库user表中所有记录
-
- List<User> user = userService.getUser();
-
- return user;
-
- }
-
-
- }

17.启动类代码
- package com.xue;
-
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @MapperScan("com.xue.repository.dao")
- public class SpringMybatisProjectApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringMybatisProjectApplication.class, args);
- }
-
- }
18.运行启动类在浏览器输入localhost:8080 /sys/acct/addUser,并查看数据库如图
19.在浏览器输入localhost:8080 /sys/acct/deleteUser,并查看数据库如图
20.在浏览器输入localhost:8080 /sys/acct/updateUser,并查看数据库如图
21.在浏览器输入localhost:8080 /sys/acct/showUser如图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。