SSM一:Mybatis最小可运行系统

这个项目总共三个类,一个pom.xml配置文件:

1.主函数,启动hsql服务器,运行create ,insert ,select等sql语句

2.mapper文件(sql写在这里面),mybatis执行的sql

3.一个简单实体类TestMybatis

pom.xml

<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>net.highersoft</groupId>
	<artifactId>mybatis.hsql</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>2.5.0</version>
		</dependency>
		<!--javassist与ognl是mybatis需要的  -->
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.24.1-GA</version>
			<scope>compile</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
			<version>3.2.10</version>
			<scope>compile</scope>
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>



主函数代码:

private static SqlSessionFactory sqlSessionFactory;

public static void startHsql() {
    new Thread() {
        //启动服务器
        public void run() {
            org.hsqldb.server.Server.main(new String[] {});
        }
    }.start();
    new Thread() {
        //启动管理页面
        public void run() {
            org.hsqldb.util.DatabaseManagerSwing.main(new String[] {});
        }
    }.start();
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void <b>main</b>(String[] args) {
    startHsql();
    
    UnpooledDataSource ds = new UnpooledDataSource();
    ds.setDriver("org.hsqldb.jdbcDriver");
    ds.setUrl("jdbc:hsqldb:hsql://localhost");
    ds.setUsername("sa");
    ds.setPassword("");
    
    
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("Test", transactionFactory, ds);
    Configuration configuration = new Configuration(environment);
    configuration.setLazyLoadingEnabled(true);
    configuration.setUseActualParamName(false); // to test legacy style reference (#{0} #{1})
    configuration.getTypeAliasRegistry().registerAlias(TestMybatis.class);
    configuration.addMapper(TestMapper.class);
    
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    SqlSession session = sqlSessionFactory.openSession(); 
    TestMapper mapper = session.getMapper(TestMapper.class);
    mapper.createTable();
    mapper.insert();
    Map<Integer,TestMybatis>    m = mapper.selectTestMybatis("test_mybatis");
    for(Entry<Integer,TestMybatis> entry:m.entrySet()) {
        System.out.println(entry.getValue().getName());
    }
        
}


下载运行下面项目文件前先确保java环境,ide(eclipse或idea),maven安装好了。

项目文件

文/程忠 浏览次数:0次   2019-11-20 14:26:48

相关阅读

微信扫描-捐赠支持
加入QQ群-技术交流

评论:
点击刷新

↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑