javabean中java
package com.sxt.bean; import org.apache.ibatis.type.Alias; public class Employee { private int id; private String last_name; private String gender; private String email; public Employee() { } public Employee(int id, String last_name, String gender, String email) { this.id = id; this.last_name = last_name; this.gender = gender; this.email = email; } @Override public String toString() { return "Employee{" + "id=" + id + ", last_name='" + last_name + '\'' + ", gender='" + gender + '\'' + ", email='" + email + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
接口中定義增刪改查的方法mysql
package com.sxt.dao; import com.sxt.bean.Employee; public interface EmployMapper { public Employee getEmpById(Integer id); public Long addEmp(Employee employee); public boolean updateEmp(Employee employee); public void deleteEmpById(Integer id); }
全局配置文件sql
<?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="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.name}" /> <property name="password" value="${jdbc.pwd}" /> </dataSource> </environment> </environments> <!-- 將咱們寫好的sql映射文件(EmployeeMapper.xml)必定要註冊到全局配置文件(mybatis-config.xml)中 --> <mappers> <mapper resource="EmployeeMapper.xml" /> </mappers> </configuration>
映射文件apache
<?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.sxt.dao.EmployMapper"> <!-- namespace:名稱空間;指定爲接口的全類名 id:惟一標識 resultType:返回值類型 #{id}:從傳遞過來的參數中取出id值 public Employee getEmpById(Integer id); --> <select id="getEmpById" resultType="com.sxt.bean.Employee"> select id,last_name,gender,email from tbl_employee where id = #{id} </select> <!--public void addEmp(Employee employee);--> <!--public void updateEmp(Employee employee);--> <!--public void deleteEmpById(Integer id);--> <!--員工添加方法--> <insert id="addEmp" parameterType="com.sxt.bean.Employee"> INSERT INTO tbl_employee(last_name,email,gender) VALUES(#{last_name},#{email},#{gender}) </insert> <update id="updateEmp"> UPDATE tbl_employee SET last_name=#{last_name},email=#{email},gender=#{gender} WHERE id=#{id} </update> <delete id="deleteEmpById"> DELETE FROM tbl_employee WHERE id=#{id} </delete> </mapper>
測試類的實現markdown
//1.根據配置的xml文件(全局配置文件,建立一個sqlsessionfactory對象,有數據源一些運行環境) public SqlSessionFactory getSqlSessionFactory () throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test03() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //1.獲取到sqlsession不會自動提交 SqlSession openSession = sqlSessionFactory.openSession(); try { EmployMapper mapper = openSession.getMapper(EmployMapper.class); //添加操做 //Employee employee = new Employee(2,"Tom","1","Tom@qq.com"); //mapper.addEmp(employee); //修改操做 Employee employee = new Employee(1,"Tom1","0","Tom1@qq.com"); boolean updateEmp= mapper.updateEmp(employee); System.out.println(updateEmp); //刪除操做 //mapper.deleteEmpById(2); //2.手動提交數據 openSession.commit(); }finally { openSession.close(); } }
獲取自增主鍵的值:
mysql支持自增主鍵,其獲取經過statement.getGenreatedKeys();session
userGeneratedKeys=「true」;使用自增主鍵獲取主鍵值策略
keyProperty:指定對應的主鍵屬性,mybatis獲取到主鍵以後,將值封裝給javaBean的哪一個屬性mybatis
核心代碼app
Employee employee = new Employee(2,"Tom","1","Tom@qq.com"); mapper.addEmp(employee); System.out.println(employee.getId());
映射的xml文件ide
insert id="addEmp" parameterType="com.sxt.bean.Employee" useGeneratedKeys="true" keyProperty="id"> INSERT INTO tbl_employee(last_name,email,gender) VALUES(#{last_name},#{email},#{gender}) </insert>
根據培訓機構視頻自學編寫的代碼測試