IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> spring(5)+mybatis -> 正文阅读

[Java知识库]spring(5)+mybatis

spring+mybatis

PersonBean和PersonBeanExample和PersonBeanMapper和PersonBeanMapper为mybatis逆向工程工具生产的。直接复制进。

需要导入的依赖包:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

??? <dependency>

????? <groupId>org.springframework</groupId>

????? <artifactId>spring-context</artifactId>

????? <version>5.3.16</version>

??? </dependency>

??? <!-- spring-jdbc -->

??? <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->

??? <dependency>

????? <groupId>org.springframework</groupId>

????? <artifactId>spring-jdbc</artifactId>

????? <version>5.3.16</version>

??? </dependency>

??? <!-- spring_tx -->

??? <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->

??? <dependency>

????? <groupId>org.springframework</groupId>

????? <artifactId>spring-tx</artifactId>

????? <version>5.3.16</version>

??? </dependency>

??? <!-- MyBatis依赖 -->

??? <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->

??? <dependency>

????? <groupId>org.mybatis</groupId>

????? <artifactId>mybatis</artifactId>

????? <version>3.5.9</version>

??? </dependency>

??? <!-- mybatis-spring 整合包 -->

??? <dependency>

????? <groupId>org.mybatis</groupId>

????? <artifactId>mybatis-spring</artifactId>

????? <version>2.0.7</version>

??? </dependency>

??? <!-- mysql数据库驱动 -->

??? <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

??? <dependency>

????? <groupId>mysql</groupId>

????? <artifactId>mysql-connector-java</artifactId>

????? <version>8.0.25</version>

??? </dependency>

??? <!--druid 阿里的连接池-->

??? <dependency>

????? <groupId>com.alibaba</groupId>

????? <artifactId>druid</artifactId>

????? <version>1.2.8</version>

</dependency>

mydata.properties

driver=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/student_db?serverTimezone=UTC

jdbc.username=root

password=123456

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

?????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

?????? xmlns:context="http://www.springframework.org/schema/context"

?????? xsi:schemaLocation="http://www.springframework.org/schema/beans

?????? http://www.springframework.org/schema/beans/spring-beans.xsd

?????? http://www.springframework.org/schema/context

?????? http://www.springframework.org/schema/context/spring-context.xsd">

<!--??? 配置自动扫描包-->

??? <context:component-scan base-package="com.weiwei.springmybatis"/>

<!-- 配置加载mydata.properties-->

??? <context:property-placeholder location="classpath:mydata.properties"/>

<!-- 配置数据源-->

??? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

??????? <property name="driverClassName" value="${driver}"/>

??????? <property name="url" value="${url}"/>

??????? <property name="username" value="${jdbc.username}"/>

??????? <property name="password" value="${password}"/>

??? </bean>

<!--??? 配置SqlSessionFactoryBean-->

??? <bean class="org.mybatis.spring.SqlSessionFactoryBean">

??????? <property name="dataSource" ref="dataSource"/>

??????? <property name="mapperLocations" value="classpath:PersonBeanMapper.xml"/>

??? </bean>

<!--??? 创建数据库访问接口对象-->

??? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

??????? <property name="basePackage" value="com.weiwei.springmybatis.mapper"/>

??? </bean>

</beans>

PersonBeanService

package com.weiwei.springmybatis.service;



import com.weiwei.springmybatis.bean.PersonBean;



import java.util.List;



public interface PersonBeanService {

??? void insertPerson(PersonBean personBean);

??? void updatePerson(PersonBean personBean);

??? void deletePerson(int perid);

??? PersonBean selectPersonById(int perid);

??? List<PersonBean> selectPerson();

??? List<PersonBean> selectPersonByNameAge(PersonBean personBean);



}

PersonBeanServiceImpl

package com.weiwei.springmybatis.service;



import com.weiwei.springmybatis.bean.PersonBean;

import com.weiwei.springmybatis.bean.PersonBeanExample;

import com.weiwei.springmybatis.mapper.PersonBeanMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;



import java.util.List;



@Service

public class PersonBeanServiceImpl implements PersonBeanService{

??? @Autowired

??? private PersonBeanMapper personBeanMapper;



??? @Override

??? public void insertPerson(PersonBean personBean) {

??????? personBeanMapper.insert(personBean);

??? }



??? @Override

??? public void updatePerson(PersonBean personBean) {

??????? personBeanMapper.updateByPrimaryKey(personBean);

??? }



??? @Override

??? public void deletePerson(int perid) {

??????? personBeanMapper.deleteByPrimaryKey(perid);

??? }



??? @Override

??? public PersonBean selectPersonById(int perid) {

??????? return personBeanMapper.selectByPrimaryKey(perid);

??? }



??? @Override

??? public List<PersonBean> selectPerson() {

??????? return personBeanMapper.selectByExample(null);

??? }



??? @Override

?? ?public List<PersonBean> selectPersonByNameAge(PersonBean personBean) {

??????? PersonBeanExample personBeanExample = new PersonBeanExample();

??????? PersonBeanExample.Criteria criteria = personBeanExample.createCriteria();

??????? if(personBean.getPersonName()!=null || personBean.getPersonName().length()>0) {

??????????? criteria.andPersonNameLike("%" + personBean.getPersonName() + "%");

??????? }

??????? if (personBean.getPersonAge()!=null){

??????????? criteria.andPersonAgeEqualTo(personBean.getPersonAge());

??????? }

??????? List<PersonBean> personBeanList = personBeanMapper.selectByExample(personBeanExample);

??????? return personBeanList;

??? }

}

PersonController

package com.weiwei.springmybatis.controller;



import com.weiwei.springmybatis.bean.PersonBean;

import com.weiwei.springmybatis.service.PersonBeanService;

import com.weiwei.springmybatis.service.PersonBeanServiceImpl;

import org.omg.CORBA.PUBLIC_MEMBER;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;



import java.util.List;



@Controller("percontrol")

public class PersonController {

??? @Autowired

??? private PersonBeanService personBeanService;

??? public Boolean addPerson(PersonBean personBean){

??????? boolean falg=false;

??????? try{

??????????? personBeanService.insertPerson(personBean);

??????????? falg=true;

??????? }catch (Exception e){

??????????? e.printStackTrace();

??????? }

??????? return falg;

??? }

??? public Boolean updatePerson(PersonBean personBean){

??????? boolean falg=false;

??????? try{

??????????? personBeanService.updatePerson(personBean);

??????????? falg=true;

??????? }catch (Exception e){

??????????? e.printStackTrace();

??????? }

??????? return? falg;

??? }



??? public Boolean deletePerson(int perid){

??????? boolean falg=false;

??????? try{

??????????? personBeanService.deletePerson(perid);

??????????? falg=true;

??????? }catch (Exception e){

??????????? e.printStackTrace();

??????? }

??????? return? falg;

??? }



???? public PersonBean selectPersonByid(int perid){

???????? PersonBean personBean=null;

???????? try{

????????????? personBean = personBeanService.selectPersonById(perid);



???? ????}catch (Exception e){

???????????? e.printStackTrace();

???????? }

??????? return? personBean;

???? }



??? public List<PersonBean> selectPerson(){

??????? return? personBeanService.selectPerson();

??? }



??? public List<PersonBean> selectPersonByAgeName(PersonBean personBean){

??????? return? personBeanService.selectPersonByNameAge(personBean);

??? }

}

Test

package com.weiwei.springmybatis.test;



import com.weiwei.springmybatis.bean.PersonBean;

import com.weiwei.springmybatis.controller.PersonController;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



import java.util.List;



public class Test {

??? public static void main(String[] args) {

??????? ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

??????? PersonController percontrol = applicationContext.getBean("percontrol", PersonController.class);

??????? /*添加

??? ????PersonBean personBean1 = new PersonBean();

??????? personBean1.setPersonName("孙悟空");

??????? personBean1.setPersonAge(1000);

??????? personBean1.setPersonAddress("花果山");

??????? percontrol.addPerson(personBean1);

??????? */





??????? /*修改

??????? PersonBean personBean2 = new PersonBean();

??????? personBean2.setPersonId(15);

??????? personBean2.setPersonName("唐僧");

??????? personBean2.setPersonAddress("东土大唐");

??????? personBean2.setPersonAge(200);

??????? percontrol.updatePerson(personBean2);

???????? */

??????? /*

??????? 根据id查询

??????? PersonBean personBean2 = new PersonBean();

??????? PersonBean personBean = percontrol.selectPersonByid(3);

??????? System.out.println("id====3,name===="+personBean.getPersonName()+"age====="+personBean.getPersonAge());

???????? */



??????? /*删除

??????? PersonBean personBean2 = new PersonBean();

??????? Boolean boo = percontrol.deletePerson(3);

??????? if (boo){

??????????? System.out.println("删除成功");

??????? }



???????? */



??????? //查询所有

??????? /*

??????? List<PersonBean> personBeanList = percontrol.selectPerson();

??????? for (PersonBean personBean:personBeanList) {

??????????? System.out.println(personBean.getPersonName());

??????? }



???????? */



??????? //按名字和年龄查询

??????? PersonBean personBean = new PersonBean();

??????? personBean.setPersonName("孙");

??????? personBean.setPersonAge(1000);

??????? List<PersonBean> personBeans = percontrol.selectPersonByAgeName(personBean);

??????? for (PersonBean personBean1:personBeans) {

??????????? System.out.println(personBean1.getPersonName()+personBean1.getPersonAge());

??????? }

??? }



}

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-03-08 22:14:58  更:2022-03-08 22:19:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 11:30:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码