添加mybatis对应maven包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
在src/main/的resources内创建【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">
添加图下配置编码:
<?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> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="com.item.model"/> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/item/mapper/TbTourinfoMapper.xml"></mapper> </mappers> </configuration>
根据【com/item/mapper/TbTourinfoMapper.xml】在【resources】下创建包与配置文件
编码内容如下,包含【查询】【筛选查询】【添加】【删除】【修改】
<?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.item.mapper.TbTourinfoMapper"> <!-- 查询所有 --> <select id="GetInfo" resultType="TbTourinfo"> select * from tb_tourinfo; </select> <!-- 各种查询--> <select id="SelectByNameSexType" resultType="TbTourinfo"> select * from tb_tourinfo <if test="name!=null or sex!=null or type!=null"> where 1=1 </if> <if test="name!=null"> and name like "%${name}%" </if> <if test="sex!=null"> and sex="${sex}" </if> <if test="type!=null"> and type="${type}" </if> </select> <insert id="AddInfo"> insert into tb_tourinfo values ( 0, "${name}", "${sex}", "${id_no}", #{age}, #{type}, NOW(), #{visit_state}, now() ); </insert> <!-- 删除语句 --> <delete id="DeleteById" parameterType="java.lang.Integer"> delete from tb_tourinfo where id=#{id} </delete> <!-- 修改状态在语句 --> <update id="UpdateById"> update tb_tourinfo set visit_state=#{visit_state} where id=#{id} </update> </mapper>
在【resources】下创建【log4j.properties】文件
#全局配置 log4j.rootLogger=debug,stdout,ERROR #MyBatis日志配直 log4j.logger.com.item.mapper=TRACE #控制台输出配置 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
创建【com.item.model】内【TbTourinfo】文件
需要添加数据表对应的【属性】,并添加【Getter】与【Setter】以及【toString】方法。
package com.item.model; import java.util.Date; public class TbTourinfo { private int id; private String name; private String sex; private String id_no; private int age; private String type; private Date visit_date; private int visit_state; private Date register_time; @Override public String toString() { return "TbTourinfo{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", id_no='" + id_no + '\'' + ", age=" + age + ", type='" + type + '\'' + ", visit_date=" + visit_date + ", visit_state=" + visit_state + ", register_time=" + register_time + '}'; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getId_no() { return id_no; } public void setId_no(String id_no) { this.id_no = id_no; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Date getVisit_date() { return visit_date; } public void setVisit_date(Date visit_date) { this.visit_date = visit_date; } public int getVisit_state() { return visit_state; } public void setVisit_state(int visit_state) { this.visit_state = visit_state; } public Date getRegister_time() { return register_time; } public void setRegister_time(Date register_time) { this.register_time = register_time; } }
在【com.item.mapper】下创建【TbTourinfoMapper】
package com.item.mapper; import com.item.model.TbTourinfo; import org.apache.ibatis.annotations.Param; import java.util.List; public interface TbTourinfoMapper { List<TbTourinfo> GetInfo(); /** * 根据参数进行查询 * @param name (姓名) * @param sex (性别) * @param type (会员类型) * @return */ List<TbTourinfo> SelectByNameSexType( @Param("name") String name, @Param("sex") String sex, @Param("type") String type ); int AddInfo( @Param("name") String name, @Param("sex") String sex, @Param("id_no") String id_no, @Param("age") int age, @Param("type") int type, @Param("visit_state") int visit_state ); int DeleteById(@Param("id") int id); int UpdateById(@Param("visit_state") int visit_state, @Param("id") int id); }
在【com.item.common】下创建JDBC用于获取数据库连接池链接
package com.item.common; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.Reader; public class JDBC { public static SqlSessionFactory GetConn(){ try { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); return factory; } catch (IOException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { System.out.println(GetConn()); } }
在【com.item.dao】下创建【TbTourinfoDAO.java】文件
package com.item.dao; import com.item.common.JDBC; import com.item.mapper.TbTourinfoMapper; import com.item.model.TbTourinfo; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class TbTourinfoDAO { /** * 查询所有 * @return */ public static List<TbTourinfo> GetInfo(){ SqlSessionFactory factory = JDBC.GetConn(); SqlSession session = factory.openSession(); TbTourinfoMapper db = session.getMapper(TbTourinfoMapper.class); List<TbTourinfo> list = db.GetInfo(); session.close(); return list; } /** * 综合信息查询 * @param name * @param sex * @param type * @return */ public static List<TbTourinfo> SelectByNameSexType(String name,String sex,String type){ SqlSessionFactory factory = JDBC.GetConn(); SqlSession session = factory.openSession(); TbTourinfoMapper db = session.getMapper(TbTourinfoMapper.class); List<TbTourinfo> list = db.SelectByNameSexType(name,sex,type); session.close(); return list; } /** * 添加 * @param name * @param sex * @param id_no * @param age * @param type * @param visit_state * @return */ public static boolean AddInfo(String name,String sex,String id_no,int age,int type,int visit_state){ SqlSessionFactory factory = JDBC.GetConn(); SqlSession session = factory.openSession(); TbTourinfoMapper db = session.getMapper(TbTourinfoMapper.class); int rows = db.AddInfo(name, sex, id_no, age, type, visit_state); session.commit(); session.close(); return rows>0; } /** * 删除 * @param id * @return */ public static boolean DeleteById(int id){ SqlSessionFactory factory = JDBC.GetConn(); SqlSession session = factory.openSession(); TbTourinfoMapper db = session.getMapper(TbTourinfoMapper.class); int rows = db.DeleteById(id); session.commit(); session.close(); return rows>0; } /** * 修改 * @param visit_state * @param id * @return */ public static boolean UpdateById(int visit_state,int id){ SqlSessionFactory factory = JDBC.GetConn(); SqlSession session = factory.openSession(); TbTourinfoMapper db = session.getMapper(TbTourinfoMapper.class); int rows = db.UpdateById(visit_state,id); session.commit(); session.close(); return rows>0; } public static void main(String[] args) { int oldState=1; System.out.println(UpdateById(oldState==0?1:0,7)); } }