美文网首页
spring boot mybaties 使用XML操作

spring boot mybaties 使用XML操作

作者: cyhai | 来源:发表于2019-10-08 17:33 被阅读0次

    现在很大部分后台开发的,在项目配置和mysql操作还是会使用到xml,所以到此记录一下使用xml操作数据的做法。

    创建新项目(IDEA)

    添加mybaties依赖

     <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    

    配置连接数据库信息

    spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/userbase?setUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=wordpress
    
    
    创建必要的类与文件

    新建一个class,命名为MysqlController,用于开放接口。
    新建一个class,命名为User,用于返回和查询数据用的model。
    新建一个interface,命名为UserServer,再新建一个Class,命名为UserServerImpl,用来实现接口功能逻辑和连接部分。
    新建一个interface,命名为UserMapper,处理数据库操作
    再新建一个xml文件放在resources的mapper中,命名为UserMapper。
    整体如下图所示


    image.png

    打开User

    public class User {
    
        public  String name;
        public  int age;
        public  int id;
        public  String sex;
    }
    

    打开UserMapper

    @Component
    public interface UserMapper {
        List<User> getAllUsers();
    }
    

    打开UserServer

    @Service
    public interface UserServer {
        List<User> getUserList();
    
    }
    

    打开UserServerImpl

    @Repository
    public class UserServerImpl implements UserServer {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> getUserList() {
    
            return userMapper.getAllUsers();
        }
    }
    

    打开MysqlController

    @RestController
    public class MysqlController {
    
        @Autowired
        private UserServerImpl userServer;
    
        @GetMapping("/getAlluser")
        public List<User>  getUsers()
        {
    
            return userServer.getUserList();
    
        }
     }
    
    

    打开DemoApplication

    @SpringBootApplication
    
    @MapperScan("cyhai.example.demo")
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    

    打开application.properties添加

    mybatis.mapper-locations= classpath:mapper/*.xml
    

    最后打开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= "cyhai.example.demo.mapper.UserMapper" >
    
        <resultMap id ="UserInfoMap" type="cyhai.example.demo.model.User">
            <result column="name_" property="name"/>
            <result column="sex" property="sex"/>
            <result column="age" property="age"/>
            <result column="id" property="id"/>
        </resultMap>
    
        <select id="getAllUsers" resultMap="UserInfoMap">
            select * from usertable
        </select>
    </mapper>
    
    把项目启动,并在浏览器键入http://localhost:8080/getAlluser
    结束

    相关文章

      网友评论

          本文标题:spring boot mybaties 使用XML操作

          本文链接:https://www.haomeiwen.com/subject/zdvnectx.html