myBatis entry CRUD
By (insert)
Join <mappers> tag in the file which joined Mapper.xml <insert> tags to add a sql statement
例:insert into t_customer
(
NAME,
gender,
telephone,
address
)
VALUES
(
#{name},
#{gender},
#{telephone},
#{address}
);
Required to write in this format corresponding to the above defined variables corresponding to the following custom.
Add <serelt> for query
select t_costomer where name LIKE "%"#{name}"%"
The best use placeholders to solve the problem
Add <update> Update achieve
update t_costomer set name = #{name} where id = #{id}
Updates to update the name specified by id
Add <delete> Delete achieve
delete from t_costomer where name = #{name}
To delete a user by name
Implemented in the test class
Create a test class Test
pubilc CustomerTest{
@Test
public void Test(){
String resource="sqlMapConfig.xml";
InputSteram in = Resource.Resources.getResourceAsStream(resource );
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = ssfb.build(in);
SqlSession session = ssf.openSession();
Customer customer =new Customer();
customer.setname("balbala");
session.insert("Mapper.insertId",customer);
session.selectOne("Mapper.insertId",1);
Fuzzy query
List<User> list = session.selectList("UserMapper.selectUserByName", "balabala");
for (User u : list) {
System.out.println(u);
}
session.update("Mapper.insertId",customer);
session.delete("Mapper.insertId",id);
in.close();
}
}