Mybatis简单的增删改查

1.创建数据库:

2. 生辰数据库实现类pojo类:

package com.sunyu.pojo;

import java.util.Objects;

public class Student {
    private Integer studentId;
    private String studentName;
    private Integer studentAge;
    private Integer math;
    private Integer chinese;
    private Integer english;
    public Student(){

    }

    public Student(Integer studentId, String studentName, Integer studentAge, Integer math, Integer chinese, Integer english) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.studentAge = studentAge;
        this.math = math;
        this.chinese = chinese;
        this.english = english;
    }

    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(Integer studentAge) {
        this.studentAge = studentAge;
    }

    public Integer getMath() {
        return math;
    }

    public void setMath(Integer math) {
        this.math = math;
    }

    public Integer getChinese() {
        return chinese;
    }

    public void setChinese(Integer chinese) {
        this.chinese = chinese;
    }

    public Integer getEnglish() {
        return english;
    }

    public void setEnglish(Integer english) {
        this.english = english;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(studentId, student.studentId) && Objects.equals(studentName, student.studentName) && Objects.equals(studentAge, student.studentAge) && Objects.equals(math, student.math) && Objects.equals(chinese, student.chinese) && Objects.equals(english, student.english);
    }

    @Override
    public int hashCode() {
        return Objects.hash(studentId, studentName, studentAge, math, chinese, english);
    }
}

3.创建交互接口mapper

package com.sunyu.mapper;

public interface StudentMapper {
//    添加学生
    int insertStudent();
    boolean deleteStudent();
}

4.创建mybatis核心配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/studentscore?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--引入mybatis的映射文件-->
    <mappers>
        <mapper resource="mapper/StudentMapper.xml"/>
    </mappers>
</configuration>

5.创建接口映射文件StudentMapper

<?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">
<!--<1&#45;&#45;namespace与mapper接口的全类名保持一致,-->
<mapper namespace="com.sunyu.mapper.StudentMapper">
<!--    添加学生-->
<!--    int insertStudent();-->
    <insert id="insertStudent">
        insert into grade
        values
        (null,"ok",23,56,46,79)
    </insert>
//删除学生
<!--    boolean deleteStudent();-->
    <delete id="deleteStudent">
        DELETE from grade where student_id = 1092
    </delete>
</mapper>

6.测试添加学生

import com.sunyu.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import solo.Giudice;

import java.io.IOException;
import java.io.InputStream;

public class StudentMapperTest {
    @Test
    public void text() throws IOException {
//        构建核心配置文件输入流
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
//        获取sql对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//        获取sqlsessionfactory对象
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
//        获取sql的会话对象sqlsession,用来操作数据库
//        true自动提交事务
        SqlSession sqlSession = build.openSession(true);
//        获取接口的代理实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        使用代理实现类调用方法
        int i = mapper.insertStudent();
        boolean result = Giudice.getResult(i);
        System.out.println(result);
//        手动添加数据库事务
//        sqlSession.commit();
        sqlSession.close();
    }
}

7.基于mybatis3.5.7pom.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>

    <groupId>org.example</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
    <!-- Mybatis核心 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
        <!-- junit测试 -->
        <dependency>
        <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- MySQL驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
</dependencies>

</project>
### 使用PINN(物理信息神经网络)解决偏微分方程的实例 #### 背景介绍 物理信息神经网络(Physics-Informed Neural Networks, PINNs)是一种结合深度学习与物理学知识的方法,能够高效求解复杂的偏微分方程(Partial Differential Equations, PDEs)。这种方法的核心在于将已知的物理规律作为约束条件嵌入到神经网络训练过程中,从而提高模型预测能力并减少对大量数据的需求。 以下是几个典型的PINN用于求解PDE的具体案例及其代码实现: --- #### 案例1:一维热传导方程 在一维空间中,热传导过程可以用如下形式表示: \[ u_t = \alpha u_{xx}, \quad x \in [a,b], t > 0, \] 其中 \(u(x,t)\) 表示温度分布,\(t\) 是时间变量,\(x\) 是位置坐标,而 \(\alpha\) 则代表材料的导热系数。边界条件可以设定为固定端点处的温度值或者绝热状态下的梯度零假设。 ##### 实现步骤 下面展示了一段基于PyTorch框架的一维热传导方程解决方案[^2]: ```python import torch import numpy as np # 定义神经网络结构 class Net(torch.nn.Module): def __init__(self, layers): super(Net, self).__init__() self.linears = torch.nn.ModuleList([torch.nn.Linear(layers[i], layers[i+1]) for i in range(len(layers)-1)]) def forward(self, x): a = x for i, l in enumerate(self.linears[:-1]): a = torch.tanh(l(a)) a = self.linears[-1](a) return a # 初始化参数 layers = [2, 20, 20, 1] # 输入维度 (x,t),隐藏层节点数,输出维度(u) model = Net(layers) def compute_loss(model, x_data, t_data, alpha=0.1): """定义损失函数""" xt = torch.cat((x_data.unsqueeze(-1), t_data.unsqueeze(-1)), dim=-1).requires_grad_(True) u_pred = model(xt) grad_u = torch.autograd.grad( outputs=u_pred.sum(), inputs=xt, create_graph=True)[0] u_x = grad_u[:, 0].view(-1, 1) u_t = grad_u[:, 1].view(-1, 1) hessian_xx = torch.autograd.grad(outputs=u_x, inputs=xt, retain_graph=True, create_graph=True)[0][:, 0].view(-1, 1) pde_residual = u_t - alpha * hessian_xx mse_pde = torch.mean(pde_residual ** 2) return mse_pde # 训练循环省略... ``` 上述代码片段展示了如何构建一个简单的全连接前馈神经网络,并通过自动微分技术计算目标函数相对于输入的空间二阶导数以及时间一阶导数,进而形成残差项以优化整个系统性能。 --- #### 案例2:Burgers' 方程 另一个经典例子是非线性的 Burgers’ 方程,在流体力学领域具有重要意义: \[ u_t + uu_x = \nu u_{xx}, \] 这里引入了粘滞效应因子 \(\nu>0\) 来描述扩散现象的影响程度。该类问题同样可以通过调整相应超参设置来适配不同场景需求[^1]。 --- #### 已验证的有效性分析 研究表明,相比于传统数值方法如有限元法或谱方法等,采用PINN不仅可以获得更高的精度而且还能显著降低运算成本特别是当面对高维情形时优势更加明显。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值