springboot中rom操作mysql数据库

首页 / 🍁编程类 / 正文

使用MyBatis框架操作数据, 在SpringBoot框架集成MyBatis

使用步骤

  1. mybatis起步依赖 : 完成mybatis对象自动配置, 对象放在容器中
  2. pom.xml 指定把src/main/java目录中的xml文件包含到classpath中
  3. 创建实体类Student
  4. 创建Dao接口 StudentDao , 创建一个查询学生的方法
  5. 创建Dao接口对应的Mapper文件, xml文件, 写sql语句
  6. 创建Service层对象, 创建StudentService接口和他的实现类。 去dao对象的方法。完成数据库的操作
  7. 创建Controller对象,访问Service。
  8. 写application.properties文件 配置数据库的连接信息。

我这边只测试了第一种,第二种和第三种未测试,不过应该按照下面的提示差不多

项目目录截图

第一种

server.port=8080
server.servlet.context-path=/student

#链接数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootdb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=911
#日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

dao层java文件和xml文件

/*
告诉mybatis这是一个结果
创建此接口的代理
 */
@Mapper
public interface StudentDao {
    Student selectById(Integer id);
}
<mapper namespace="com.xiaonan.springboot.dao.StudentDao">
    <!--
      定义sql语句
     -->
    <select id="selectById" resultType="com.xiaonan.springboot.pojo.Student">
        select id,name,age from student where id=#{id}
    </select>
</mapper>

student的pojo类

public class Student {

    Integer id;
    String name;
    Integer age;

    public Student() {

    }

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

service中接口文件和实现类

public interface StudentService {
    Student selectById(Integer id);
}
@Service
public class StudentImpl implements StudentService {
    @Resource
    StudentDao studentDao;
    @Override
    public Student selectById(Integer id) {
        Student student=studentDao.selectById(id);
        return  student;
    }
}

控制层

@Controller
public class StudentController {

    @Resource
    StudentService studentService;

    @RequestMapping("/studentid")
    @ResponseBody
    public String selectID(int id){
        Student student=studentService.selectById(id);
        return  student.toString();
    }
}

springboot中启动类

@SpringBootApplication
public class SpringbootMapperApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMapperApplication.class, args);
    }
}

第一种方式总结几点

总结经验

  1. dao层:分为接口和xml文件,其中接口上面必须使用mapper注解,配置文件使用方法和mybatis一样。
  2. service层:分为接口和接口实现类,必须在使用类上面用service注解,并且下面调用dao层时使用Resource注解
  3. 控制层:没有什么要说的了,注解使用和之前的一样
  4. properties文件里面,需要声明url,username,驱动等
  5. 日志反馈使用:mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

第二种方式 @MapperScan

其实简单来说,就是把dao层的接口和mapper的xml文件分别放在一个包里面,与其这样,还不如使用第三种

/**
 * @MapperScan: 找到Dao接口和Mapper文件
 * basePackages:Dao接口所在的包名
 */
@SpringBootApplication
@MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"})
public class Application {
}

第三种方式: Mapper文件和Dao接口分开管理

1)在resources目录中创建子目录 (自定义的) ,例如mapper
2)把mapper文件放到 mapper目录中
3)在application.properties文件中,指定mapper文件的目录

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4)在pom.xml中指定 把resources目录中的文件 , 编译到目标目录中

<!--resources插件-->
<resources>
   <resource>
      <directory>src/main/resources</directory>
      <includes>
         <include>**/*.*</include>
      </includes>
   </resource>
</resources>

文章全部是本人原创,请勿转发,谢谢配合,版权所有-南香香-你会喜欢我吗

评论区
头像