首页 > 应用开发 > Java > 走进JavaWeb技术世界14:Mybatis入门
select * from products where id = #{id}
4.0.0 com.sl mybatis-demo 0.0.1-SNAPSHOT 4.12 3.4.1 5.1.32 1.2.17 junit junit ${junit.version} org.mybatis mybatis ${mybatis.version} mysql mysql-connector-java ${mysql.version} log4j log4j ${log4j.version}
select * from products
//使用productMapper.xml配置文件 public class TestClient { //定义会话SqlSession SqlSession session =null; @Before public void init() throws IOException { //定义mabatis全局配置文件 String resource = "SqlMapConfig.xml"; //加载mybatis全局配置文件 //InputStream inputStream = TestClient.class.getClassLoader().getResourceAsStream(resource); InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream); //根据sqlSessionFactory产生会话sqlsession session = factory.openSession(); } //查询所有user表所有数据 @Test public void testSelectAllUser() { String statement = "com.sl.mapper.ProductMapper.selectAllProduct"; List listProduct =session.selectList(statement); for(Product product:listProduct) { System.out.println(product); } //关闭会话 session.close(); } }
public class Product { private int Id; private String Name; private String Description; private BigDecimal UnitPrice; private String ImageUrl; private Boolean IsNew; 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 getDescription() { return Description; } public void setDescription(String description) { this.Description = description; } public BigDecimal getUnitPrice() { return UnitPrice; } public void setUnitPrice(BigDecimal unitprice) { this.UnitPrice = unitprice; } public String getImageUrl() { return Name; } public void setImageUrl(String imageurl) { this.ImageUrl = imageurl; } public boolean getIsNew() { return IsNew; } public void setIsNew(boolean isnew) { this.IsNew = isnew; } @Override public String toString() { return "Product [id=" + Id + ", Name=" + Name + ", Description=" + Description + ", UnitPrice=" + UnitPrice + ", ImageUrl=" + ImageUrl + ", IsNew=" + IsNew+ "]"; } }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69906029/viewspace-2661337/ ,如需转载,请注明出处,否则将追究法律责任。
注册时间:2019-01-14