In a project, the spring agent is usually used to manage datasource and so on. Make full use of spring interface-based programming and the convenience brought by aop and ioc. There are many similarities between using spring to manage mybatis and hibernate. The focus today is data source management and bean configuration.
First, make a change to the previous project structure, create a folder config under the src_user source code directory, and configure the original mybatis Configuration file. xml is moved to this folder, and the spring configuration file: applicationContext is created in the config folder. xml, the most important configuration in this configuration file:
<! -- In this example, the DBCP connection pool is used. Copy the jar package of DBCP to the lib Directory of the project in advance. --> <Bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "> <property name =" driverClassName "value =" com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // 127.0.0.1: 3306/mybatis? CharacterEncoding = utf8 "/> <property name =" username "value =" root "/> <property name =" password "value =" password "/> </bean> <bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <! -- DataSource attribute specifies the connection pool to be used --> <property name = "dataSource" ref = "dataSource"/> <! -- ConfigLocation attribute specifies the core Configuration file of mybatis --> <property name = "configLocation" value = "config/Configuration. xml "/> </bean> <bean id =" userMapper "class =" org. mybatis. spring. mapper. mapperFactoryBean "> <! -- SqlSessionFactory attribute specifies the SqlSessionFactory instance to be used --> <property name = "sqlSessionFactory" ref = "sqlSessionFactory"/> <! -- MapperInterface attribute specifies the ER interface, which is used to implement this interface and generate the er object --> <property name = "mapperInterface" value = "com. yihaomen. mybatis. inter. IUserOperation "/> </bean>
Here, the focus is org. mybatis. spring. SqlSessionFactoryBean and org. mybatis. spring. mapper. MapperFactoryBean [B] implement spring interfaces and generate objects. For details, see the mybatis-spring code. (Http://code.google.com/p/mybatis/), if you only use, fixed mode, this configuration is fine.
Import java. util. list; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. yihaomen. mybatis. inter. IUserOperation; import com. yihaomen. mybatis. model. article; import com. yihaomen. mybatis. model. user; public class MybatisSprintTest {private static ApplicationContext ctx; static {ctx = new ClassPathXmlApplicationContext ("config/applicationContext. xml ");} public static void main (String [] args) {IUserOperation mapper = (IUserOperation) ctx. getBean ("userMapper"); // user query with test id = 1. You can change it to your own according to the database conditions. system. out. println ("obtain User information with user id = 1"); User user User = mapper. selectUserByID (1); System. out. println (user. getUserAddress (); // get the article list to test System. out. println ("List of all articles whose user ID is 1"); List <Article> articles = mapper. getUserArticles (1); for (Article article: articles) {System. out. println (article. getContent () + "--" + article. getTitle ());}}}
Here, the focus is org. mybatis. spring. SqlSessionFactoryBean and org. mybatis. spring. mapper. MapperFactoryBean [B] implement spring interfaces and generate objects. For details, see the mybatis-spring code. (Http://code.google.com/p/mybatis/), if you only use, fixed mode, this configuration is fine.
Import java. util. list; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. yihaomen. mybatis. inter. IUserOperation; import com. yihaomen. mybatis. model. article; import com. yihaomen. mybatis. model. user; public class MybatisSprintTest {private static ApplicationContext ctx; static {ctx = new ClassPathXmlApplicationContext ("config/applicationContext. xml ");} public static void main (String [] args) {IUserOperation mapper = (IUserOperation) ctx. getBean ("userMapper"); // user query with test id = 1. You can change it to your own according to the database conditions. system. out. println ("obtain User information with user id = 1"); User user User = mapper. selectUserByID (1); System. out. println (user. getUserAddress (); // get the article list to test System. out. println ("List of all articles whose user ID is 1"); List <Article> articles = mapper. getUserArticles (1); for (Article article: articles) {System. out. println (article. getContent () + "--" + article. getTitle ());}}}