• Springboot整合Mybatis增删改查


    1,POM文件加入依赖

       <dependencies>
            <!--Thymeleaf启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--Mybatis启动器-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.2</version>
            </dependency>
            <!--MySql驱动坐标-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            <!--Druid数据源依赖-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.12</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <!--配置Generator插件-->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.5</version>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.38</version>
                        </dependency>
                    </dependencies>
                    <!--指定配置文件的路径-->
                    <configuration>
                        <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
            <!--配置资源拷贝插件-->
            <resources>
                <resource>
                    <!--指定去哪拷贝-->
                    <directory>src/main/java</directory>
                    <includes>
                        <!--指定拷贝什么文件-->
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <!--当配置了src/main/java下的就不会拷贝resource下的文件了所以还需要配置一下-->
                <resource>
                    <!--指定去哪拷贝-->
                    <directory>src/main/resource</directory>
                    <includes>
                        <!--指定拷贝什么文件-->
                        <include>**/*.yml</include>
                        <include>**/*.propreties</include>
                    </includes>
                </resource>
            </resources>
        </build>

    2,SpringBoot配置文件

      

    3,配置generator

      

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    <generatorConfiguration>
        <!-- 数据库驱动包位置 -->
    <!--    <classPathEntry location="D:generatormysql-connector-java-5.1.18-bin.jar" />-->
        <!--id是自己起的名字-->
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <!--是否去除自动生成的注释 true:是 false:否-->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!-- 数据库链接URL、用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/demo" userId="root" password="123.com">
            </jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
            <!-- 生成模型的包名和位置 -->
            <javaModelGenerator targetPackage="com.bdqn.springbootmybatis.pojo" targetProject=".srcmainjava">
                <!--enableSubPackages是否让schema作为包的后缀名-->
                <property name="enableSubPackages" value="true" />
                <!--从数据库返回的值清理前后的空格-->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- 生成的映射文件包名和位置 -->
            <sqlMapGenerator targetPackage="com.bdqn.springbootmybatis.mapper" targetProject=".srcmainjava">
                <!--enableSubPackages是否让schema作为包的后缀名-->
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置 -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.bdqn.springbootmybatis.mapper" targetProject=".srcmainjava">
                <!--enableSubPackages是否让schema作为包的后缀名-->
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
            <table tableName="user" domainObjectName="Table1" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    
        </context>
    </generatorConfiguration>

      运行generator

          

        运行后生成4个类

          

    4,跳转页面Controller

      

     5,service层

      

       

    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
        @Override
        public void addUser(User user) {
            this.userMapper.insert(user);
        }
    
        @Override
        public List<User> showUser() {
            UserExample example=new UserExample();
            return this.userMapper.selectByExample(example);
        }
    
        @Override
        public User preUptateUser(int id) {
            return this.userMapper.selectByPrimaryKey(id);
        }
    
        @Override
        @Transactional
        public void updateUser(User user) {
            this.userMapper.updateByPrimaryKey(user);
        }
    
        @Override
        public void deleteUser(int id) {
            this.userMapper.deleteByPrimaryKey(id);
        }
    }

    6,Controller层

      

    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @PostMapping("/addUser")
        public String addUser(User user, Model model)
        {
            this.userService.addUser(user);
            return "redirect:/ok";
        };
        @GetMapping("/findUser")
        public String findUser(Model model)
        {
            List<User> list = userService.showUser();
            model.addAttribute("list",list);
            return "showUsers";
        };
        @GetMapping("/petUpdateUser")
        public String petUpdateUser(int id,Model model)
        {
            User user = this.userService.preUptateUser(id);
            model.addAttribute("user",user);
            return "updateUser";
        };
        @PostMapping("/updateUser")
        public String updateUser(User user)
        {
            this.userService.updateUser(user);
            return "redirect:/ok";
        };
        @GetMapping("/deleteUser")
        public String deleteUser(int id)
        {
            this.userService.deleteUser(id);
            return "redirect:/ok";
        };
    }

     7,启动类加入

  • 相关阅读:
    MySQL关键性能监控(QPS/TPS)
    Python小技巧
    Redis高可用演进(一)
    防范XSS攻击
    java引用知识
    ehcache同步原理
    ehcache监控
    SecureCRT使用技巧
    JUC整理笔记五之梳理Varhandle(下)
    JUC整理笔记四之梳理VarHandle(上)
  • 原文地址:https://www.cnblogs.com/yz-bky/p/12814825.html
Copyright © 2020-2023  润新知