mybatis简单示例
程序员文章站
2022-04-08 17:51:58
1、引入mybatis.jar mysql-connector-java-5.1.47.jar 2、新增数据库资源文件datasource.properties 3、新增mybatis.xml 4、新增UserDomain.java 5、新增UserMapper.java 6、新增UserMappe ......
1、引入mybatis.jar mysql-connector-java-5.1.47.jar
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.47</version>
</dependency>
2、新增数据库资源文件datasource.properties
jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8
jdbc.username=root
jdbc.password=
3、新增mybatis.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>
<properties resource="datasource.properties"/>
<environments default="dev">
<environment id="dev">
<transactionmanager type="jdbc"></transactionmanager>
<datasource type="pooled">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</datasource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/usermapper.xml"/>
</mappers>
</configuration>
4、新增userdomain.java
package cn.scp.mybatis.native_mybatis.domain; import lombok.data; import java.util.date; @data public class userdomain { private int id; private string name; private int age; private string idcard; private string school; private string job; private string remark; private date createdate; private date modifydate; private int deleteflag; }
5、新增usermapper.java
package cn.scp.mybatis.native_mybatis.mapper; import cn.scp.mybatis.native_mybatis.domain.userdomain; /** * @description * @date 2019/3/17 10:30 */ public interface usermapper { public userdomain selectbyid(int id); }
6、新增usermapper.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="cn.scp.mybatis.native_mybatis.mapper.usermapper"> <select id="selectbyid" resulttype="cn.scp.mybatis.native_mybatis.domain.userdomain"> select * from t_user_info where id=#{id} </select> </mapper>
7、新增mybatis工具类mybatisutil.java
package cn.scp.mybatis.native_mybatis.util; 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 java.io.reader; public class mybatisutil { public static sqlsessionfactory sessionfactory; static { try{ reader reader = resources.getresourceasreader("mybatis.xml"); sessionfactory = new sqlsessionfactorybuilder().build(reader); } catch (exception e){ system.out.println(e); } } public static sqlsession getsession(){ return sessionfactory.opensession(); } }
8、测试
package cn.scp.mybatis.native_mybatis; import cn.scp.mybatis.native_mybatis.domain.userdomain; import cn.scp.mybatis.native_mybatis.mapper.usermapper; import cn.scp.mybatis.native_mybatis.util.mybatisutil; import org.apache.ibatis.session.sqlsession; public class main { public static void main(string[] args) { sqlsession session = mybatisutil.getsession(); usermapper usermapper = session.getmapper(usermapper.class); try{ userdomain userdomain = usermapper.selectbyid(8); system.out.println(userdomain); } catch (exception e){ system.out.println(e); } } }
结果:
userdomain(id=8, name=7, age=11, idcard=null, school=鱼小学, job=null, remark=null, createdate=null, modifydate=null, deleteflag=0)
上一篇: 能够获得搜索引擎垂青的网页
下一篇: Java操作HBase