- 第一步:导入jar包
- 第二步:创建实体类
- 第三步:创建dao层接口,mapper映射文件
@Repository("adminDAO")
public interface AdminDAO {
/*
AdminDAO接口 可以按名称直接调用admin.xml配置文件的SQL语句
*/
// 插入数据 调用entity包admin.xml里的insertAdmin配置 返回值0(失败),1(成功)
public int insertAdmin(Admin admin);
//更新数据 调用entity包admin.xml里的updateAdmin配置 返回值0(失败),1(成功)
public int updateAdmin(Admin admin);
// 删除数据 调用entity包admin.xml里的deleteAdmin配置 返回值0(失败),1(成功)
public int deleteAdmin(String admin_id);
// 查询全部数据 调用entity包admin.xml里的getAllAdmin配置 返回List类型的数据
public List<Admin> getAllAdmin();
// 按照Admin类里面的值精确查询 调用entity包admin.xml里的getAdminByCond配置 返回List类型的数据
public List<Admin> getAdminByCond(Admin admin);
// 按照Admin类里面的值模糊查询 调用entity包admin.xml里的getAdminByLike配置 返回List类型的数据
public List<Admin> getAdminByLike(Admin admin);
// 按主键查询表返回单一的Admin实例 调用entity包admin.xml里的getAdminById配置
public Admin getAdminById(String admin_id);
}
public class City implements Serializable {
private String city_id = "C" + VeDate.getStringId();
private String city_name;
public String getCity_id() {return city_id;}
public void setCity_id(String city_id) {this.city_id = city_id;}
public String getCity_name() {return city_name;}
public void setCity_name(String city_name) {this.city_name = city_name;}}
<?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">
<!-- 配置Mabatis映射文件 -->
<mapper namespace="com.dao.CityDAO">
<resultMap type="city" id="city_Map">
<id property="city_id" column="city_id" />
<result property="city_name" column="city_name" />
</resultMap>
<!-- 插入语句 DAO通过id调用此配置 -->
<insert id="insertCity" parameterType="city">
insert into city(city_id , city_name ) values(#{city_id} , #{city_name} )
</insert>
<!-- 更新语句 DAO通过id调用此配置 -->
<update id="updateCity" parameterType="city">
update city set city_name=#{city_name} where city_id=#{city_id}
</update>
<!-- 按主键删除 DAO通过id调用此配置 -->
<delete id="deleteCity" parameterType="String">
delete from city where city_id = #{city_id}
</delete>
<!-- 查询全部信息 DAO通过id调用此配置 -->
<select id="getAllCity" resultMap="city_Map">
select a.* from city a where 1=1 order by city_id desc
</select>
…………………………………………………………………………………
</mapper>
- 第四步:创建service接口,实现类
Service接口:
@Service("adminService")
public interface AdminService {
// 插入数据 调用adminDAO里的insertAdmin配置
public int insertAdmin(Admin admin);
// 更新数据 调用adminDAO里的updateAdmin配置
public int updateAdmin(Admin admin);
// 删除数据 调用adminDAO里的deleteAdmin配置
public int deleteAdmin(String admin_id);
// 查询全部数据 调用adminDAO里的getAllAdmin配置
public List<Admin> getAllAdmin();
// 按照Admin类里面的字段名称精确查询 调用adminDAO里的getAdminByCond配置
public List<Admin> getAdminByCond(Admin admin);
// 按照Admin类里面的字段名称模糊查询 调用adminDAO里的getAdminByLike配置
public List<Admin> getAdminByLike(Admin admin);
// 按主键查询表返回单一的Admin实例 调用adminDAO里的getAdminById配置
public Admin getAdminById(String admin_id);
}
Service接口实现类:
@Service("adminService")
public class AdminServiceImpl implements AdminService {
@Resource
@Autowired
private AdminDAO adminDAO;
@Override
public int insertAdmin(Admin admin) {
return this.adminDAO.insertAdmin(admin);
}
@Override
public int updateAdmin(Admin admin) {
return this.adminDAO.updateAdmin(admin);
}
@Override
public int deleteAdmin(String admin_id) {
return this.adminDAO.deleteAdmin(admin_id);
}
@Override
public List<Admin> getAllAdmin() {
return this.adminDAO.getAllAdmin();
}
@Override
public List<Admin> getAdminByCond(Admin admin) {
return this.adminDAO.getAdminByCond(admin);
}
@Override
public List<Admin> getAdminByLike(Admin admin) {
return this.adminDAO.getAdminByLike(admin);
}
@Override
public Admin getAdminById(String admin_id) {
return this.adminDAO.getAdminById(admin_id);
}}
- 第五步:编写mybatis.xml
<?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>
<typeAliases>
<typeAlias alias="admin" type="com.entity.Admin" />
<typeAlias alias="users" type="com.entity.Users" />
<typeAlias alias="article" type="com.entity.Article" />
<typeAlias alias="car" type="com.entity.Car" />
<typeAlias alias="city" type="com.entity.City" />
<typeAlias alias="vehicle" type="com.entity.Vehicle" />
<typeAlias alias="distribution" type="com.entity.Distribution" />
<typeAlias alias="cart" type="com.entity.Cart" />
<typeAlias alias="orders" type="com.entity.Orders" />
<typeAlias alias="details" type="com.entity.Details" />
<typeAlias alias="topic" type="com.entity.Topic" />
</typeAliases>
<mappers>
<mapper resource="com/entity/admin.xml" />
<mapper resource="com/entity/users.xml" />
<mapper resource="com/entity/article.xml" />
<mapper resource="com/entity/car.xml" />
<mapper resource="com/entity/city.xml" />
<mapper resource="com/entity/vehicle.xml" />
<mapper resource="com/entity/distribution.xml" />
<mapper resource="com/entity/cart.xml" />
<mapper resource="com/entity/orders.xml" />
<mapper resource="com/entity/details.xml" />
<mapper resource="com/entity/topic.xml" />
</mappers>
</configuration>
- 第六步:编写springmvc-servlet.xml文件
<!--spring与mybatis的整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="typeAliasesPackage" value="com.entity"/>
</bean>