Skip to content

3.执行静态sql

FuriousPws002 edited this page Apr 8, 2024 · 1 revision

代码分支:03-static-sql-execution

执行简单无参保存方法,user表ddl如下

CREATE TABLE `user` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT NULL,
  `age` int DEFAULT NULL,
  `brith` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

执行序列图如下 执行序列图

单元测试:

public class SqlSessionTest {

    @Test
    public void staticSqlExecution() {
        Configuration configuration = new Configuration();
        configuration.setDataSource(DataSourceBuilderTest.build());
        configuration.addMapper(UserMapper.class);
        SqlSession sqlSession = new DefaultSqlSession(configuration);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.insert();
    }
}

其中DataSourceBuilderTest类中密码设置为自己本地MySQL密码

Clone this wiki locally