Mybatis详细教程入门(mybatis入门菜鸟教程)
605
2022-05-29
MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下
mybatis-config.xml
mysqlconfig.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8 jdbc.username=root jdbc.password1=szc.2020 orcl.driver=oracle.jdbc.OracleDriver orcl.url=jdbc:oracle:thin:@localhost:1521:orcl orcl.username=scott orcl.password=123456
EmployeeMapper.xml
EmployeeMapper & EmployeeMapperAnnotation
public interface EmployeeMapper { public Employee getEmpById(Integer id); } public interface EmployeeMapperAnnotation { @Select("select * from tbl_employee where id=#{id}") public Employee getEmpById(Integer id); }
@Alias("emp") public class Employee { private Integer id; private String lastName; private String email; private String gender; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]"; } }
测试
public class EmployeeMapperTest { @Test public void getEmpById() { SqlSession session = MybatisUtils.getSqlSession(); System.out.println(session); EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); Employee employee = mapper.getEmpById(1); System.out.println(employee); session.close(); } @Test public void test2(){ SqlSession session = MybatisUtils.getSqlSession(); EmployeeMapperAnnotation mapper = session.getMapper(EmployeeMapperAnnotation.class); Employee employee = mapper.getEmpById(1); System.out.println(employee); } }
Java
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。