Mybaits-从零开始-Hello World(暂不考虑命名规范化)
1、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="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.0.131:1521:portal" /> <property name="username" value="mybatis" /> <property name="password" value="mybatis" /> </dataSource> </environment> </environments> <!-- mapping 文件路径配置 --> <mappers> <mapper resource="com/myOrg/deptUseMapper.xml" /> </mappers> </configuration>
2、deptUseMapper.xml
<?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"> <mapper namespace="com.myOrg.deptUseMapper"> <select id="getDeptUserByID" resultType="com.myOrg.DeptUserDomain"> select * from deptUser where id='46df5041-5fe2-4bce-aaa1-9031ab929df4' </select> <insert id="save" parameterType="com.myOrg.DeptUserDomain"> insert into deptUser value(1,2,3) </insert> </mapper>
3、DeptUserDomain.java
package com.myOrg; import java.sql.Date; public class DeptUserDomain { String name; Date hireDate; float salary; String id; public void setId(String id) { this.id = id; } public String getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } }
4、DeptUserOpt.java
package com.myOrg; import org.apache.ibatis.session.SqlSession; public class DeptUserOpt { public static void main(String[] args) { // TODO Auto-generated method stub SqlSession session=DeptUserUtil.getSession(); DeptUserDomain domain=session.selectOne("com.myOrg.deptUseMapper.getDeptUserByID"); System.out.println(domain.getName()); session.commit(); session.close(); } }
5、DeptUserUtil.java
package com.myOrg; import org.apache.ibatis.session.SqlSession; public class DeptUserOpt { public static void main(String[] args) { // TODO Auto-generated method stub SqlSession session=DeptUserUtil.getSession(); DeptUserDomain domain=session.selectOne("com.myOrg.deptUseMapper.getDeptUserByID"); System.out.println(domain.getName()); session.commit(); session.close(); } }
6、目录
7、数据表
8、运行结果