使用annotation完成对sql的获取
mybatis-config.xml文件
<configuration> <properties resource="db.properties"/> <typeAliases> <!-- 两种方法随便用,建议用第二种 --> <!--<typeAlias type="com.huawei.bean.Student" alias="Student"/>--> <package name="com.huawei.bean"/> </typeAliases> <environments default="development"> <environment id="development"> <!-- 配置事务 --> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper class="com.huawei.mapper.StudentMapper"/> </mappers> </configuration>
StudentMapper.java文件
package com.huawei.mapper; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import com.huawei.bean.Student; public interface StudentMapper { @Insert("insert into student (id,name,grade,classname) values(seq_student_id.nextval,#{name},#{grade},#{className})") public void add(Student bean); @Select("select id as id ,name as name,grade as grade, classname as className from student") public List<Student> getStudents(); }
项目结构
时间: 2024-12-29 07:31:06