package com.itheima.mapper;
import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface BrandMapper {
// 查询所有
List
@Select("select *from brand where id=#{id}")
Brand selectById(int id);
List
void add(Brand brand);
int update(Brand brand);
void deleteById(int id);
}
package com.itheima.pojo;
public class Brand {
private int id;
private String brandName;
private String companyName;
private int ordered;
private String description;
private int status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
<select id="selectAll" resultType="brand">
SELECT id, brand_name AS brandName, company_name AS companyName, ordered, description, status
FROM brand
</select>
<select id="selectByCondition" resultType="brand">
SELECT *
FROM brand
WHERE status=#{status}
and brand_name like #{brandName}
</select>
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into brand(brand_name,company_name,ordered,description,status)
values(#{brandName},#{companyName},#{ordered},#{description},#{status})
</insert>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>./logs/mybatis.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.mybatis" level="DEBUG"/>
<logger name="java.sql" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
<!-- 配置映射文件或映射类 需要修改-->
<!-- Usermapper路径-->
<mappers>
<!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<package name="com.itheima.mapper"/>
</mappers>
import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
public static void main(String[] args) throws IOException {
int id=6;
// //模糊处理
//
// brandName = "%" + brandName + "%";
//获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession对象
SqlSession sqlSession= sqlSessionFactory.openSession(true);
//获取Mapper接口对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//调用方法
Brand brand=brandMapper.selectById(1);
System.out.println(brand);
sqlSession.close();
}
}
新手小白,如有不对请指正
标签:mybaties,status,String,brand,基础,id,brandName,public From: https://www.cnblogs.com/ytrkkaa/p/18575083