标签:sql语句 pom java except class 文件 ppm 代码 div
1.先下载Mybatis和mysql connecrt的jar包
下载地址:
链接: https://pan.baidu.com/s/1kVFfF8N 密码: ypkb
导入jar包,maven的话可以直接配置pom
2.建立目录
如图所示
1.configuration.xml
在根目录里面创建这个mybatis配置文件
代码为:
<?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> <typeAliases> <!-- 设置typeAlias是为了在AppMapper.xml中不用每次写全App类的路径,而是用App代替 --> <typeAlias type="domain.App" alias="App" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://222.201.145.215/nziotdb"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <!-- 配置AppMapper.xml的路径 --> <mapper resource="mapperXml/AppMapper.xml" /> </mappers> </configuration>
其中里面配置了数据库服务器的地址,账号和密码,并且设置了映射文件mapperxml的路径
2.在domain下面建立实体类
package domain; import java.io.Serializable; public class App implements Serializable{ /** * */ private String appId; private String appDesc; private String newestVersion; private String newestVersionDesc; private String url; private String md5; public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getAppDesc() { return appDesc; } public void setAppDesc(String appDesc) { this.appDesc = appDesc; } public String getNewestVersion() { return newestVersion; } public void setNewestVersion(String newestVersion) { this.newestVersion = newestVersion; } public String getNewestVersionDesc() { return newestVersionDesc; } public void setNewestVersionDesc(String newestVersionDesc) { this.newestVersionDesc = newestVersionDesc; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMd5() { return md5; } public void setMd5(String md5) { this.md5 = md5; } public String toString() { return "App [appId=" + appId + ", appDesc=" + appDesc + ", newestVersion=" + newestVersion + ", newestVersionDesc=" + newestVersionDesc + ", url=" + url + ", md5=" + md5 + "]"; } }
这个类即代表一个表,类里面的变量可以直接设置为何表里面的字段一致,不一致就要在映射文件里面设置对应关系
3.在mapperXml下面写映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper.AppMapper"> <resultMap type="App" id="appResultMap"> <id property="appId" column="app_id" /> <result property="appDesc" column="app_desc" /> <result property="newestVersion" column="newest_version" /> <result property="newestVersionDesc" column="newest_version_desc" /> <result property="url" column="url" /> <result property="md5" column="md5" /> </resultMap> <!-- select语句 --> <select id="selectUserByID" parameterType="int" resultMap="appResultMap"> select * from app_t where app_t.app_id = #{appId} </select> </mapper>
其中resultMap就设置了对应的关系,解决实体类的变量和数据库中字段不一致的问题
具体的sql语句也是在映射文件里面写,其中id代表的是对应的方法,parameterType就是传入变量的类型,resultMap就是返回的数据类型
4.在mapper下为每个实体类建立对应的接口,接口里面的方法和映射文件里面的id一一对应
package mapper; import java.util.List; import domain.App; public interface AppMapper { public App selectUserByID(int id); public List<App> selectUsersByName(String userName); public void addUser(App user); public void updateUser(App user); public void deleteUser(int id); }
5.编写数据库查询帮助类,用于返回数据库对象
package Deal; import domain.App; import mapper.AppMapper; import org.apache.ibatis.session.SqlSession; public class Test{ public void getUserByID(int userID) { SqlSession session = SqlSessionHelper.getSessionFactory().openSession(); try { AppMapper userOperation = session .getMapper(AppMapper.class); App user = userOperation.selectUserByID(userID); if (user != null) { System.out.println(user.getAppDesc()); } } finally { session.close(); } } public static void main(String[] args) { try { Test test = new Test(); test.getUserByID(1); } catch (Exception e) { System.out.println(e.getMessage()); } } }
数据库表如下,使用该教程之前需要先在数据库建立表
查询的是app_id,对应的app_desc
运行test,得到的接口也是3
6.附上demo工程,不清楚的同学可以参考一下
链接: https://pan.baidu.com/s/1dFqVtSX 密码: jb7j
如何使用mybatis对mysql数据库进行操作,batis的增删改查
标签:sql语句 pom java except class 文件 ppm 代码 div
原文地址:http://www.cnblogs.com/caimuqing/p/6269681.html