标签:config OLE 数据库 session tac mamicode hone doc color
(3)建立com.zk.dao包下的IUser接口:
IUser.java
package com.zk.dao; import org.apache.ibatis.annotations.Param; import com.zk.pojo.User; public interface IUser { public User findUserWithNameLike(@Param("id")int id, @Param("name")String name); }
(4)在com.zk.pojo包下建立User实现IUser接口中的方法,并配置UserMapper.xml
User.java
package com.zk.pojo; public class User { private int id; private String name; private String level; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", level=" + level + ", phone=" + phone + "]"; } }
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="com.zk.dao.IUser"> <select id="findUserWithNameLike" resultType="User"> SELECT * FROM user WHERE id = #{id} <if test="name!= null"> AND name like #{name} </if> </select> </mapper>
(5)配置config/configure.xml
configure.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> <typeAliases> <typeAlias alias="User" type="com.zk.pojo.User" /> </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://localhost:3306/my"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- // power by http://www.yiibai.com --> <mapper resource="com/zk/pojo/UserMapper.xml" /> </mappers> </configuration>
(6)实现main函数
MainApp.java
package Main; import java.io.IOException; import java.io.Reader; 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 com.zk.dao.IUser; import com.zk.pojo.User; public class MainApp { private static SqlSessionFactory sqlsessionfactory; private static Reader reader; static{ try { reader=Resources.getResourceAsReader("config/configure.xml"); sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public SqlSessionFactory getSqlSessionFactory() { return sqlsessionfactory; } public static void main(String[]args) { select(1,"c"); } public static void select(int id,String name){ //获取session连接 SqlSession session=sqlsessionfactory.openSession(); //获取mapper IUser usermap=session.getMapper(IUser.class); //selectAllUser(); User user = usermap.findUserWithNameLike(id, name); //执行查询 System.out.println(user.toString()); session.commit(); //selectAllUser(); } }
Main执行结果如下:
(未完待续)
标签:config OLE 数据库 session tac mamicode hone doc color
原文地址:https://www.cnblogs.com/longlyseul/p/11273626.html